簡體   English   中英

如何在控制器外的sailsjs中訪問我的模型?

[英]How can I access my models in sailsjs outside a controller?

我有一個看起來像這樣的控制器。

module.exports = {

    readbyslug: function (req, res) {
    var slug = req.param('id');
    Store.findOneByName(slug.split('-').join(' '))
      .then(function(err, store){
      if(err)
        res.send({status:'error', msg:'no store'});
      res.send(store);

    });
  }
};

但是,我寧願不在控制器中進行所有這些登錄,我寧願將它們粘貼在另一個模塊中。

我的問題是,我如何訪問模型? 他們是全球性的還是什么?

Chadams--模型默認是全局的。 因此,如果您使用sails generate model foo ,則可以在代碼中訪問Foo 如果你不想訪問他們的方式,你可以使用sails引用來訪問sails.models.foo ,這是同樣的事情。

希望有所幫助!

順便說一句,通過自定義sails.config.globals可以禁用此自動全球化。 例如:

// To configure what's available globally, make a new file, `config/globals.js`
// with the following contents:

// In this example, I'll disable globalization of models, as well as _ and async.
// (But I'll leave the `sails` global available.)

// Variables which will be made globally accessible to the server process
module.exports.globals = {
  _ : false,
  async: false,
  models: false,
  sails: true
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM