我有一个看起来像这样的 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);
});
}
};
但是,我宁愿不要在 Controller 中进行所有这些登录,我宁愿将它们放在另一个模块中。
我的问题是,如何访问模型?它们是全局性的还是什么?
最佳答案
Chadams——默认情况下模型是全局的。因此,如果您执行了 sails generate model foo
,则可以在代码中访问 Foo
。如果您不想以这种方式访问它们,您可以使用 sails
引用来访问 sails.models.foo
,这是同一回事。
希望对您有所帮助!
btw this auto-globalization is disable-able by customizing
sails.config.globals
. For example:
// 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
};
关于node.js - 如何在 Controller 外部访问 sailsjs 中的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18617716/