node.js - 如何在 Controller 外部访问 sailsjs 中的模型?

标签 node.js sails.js

我有一个看起来像这样的 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/

相关文章:

css - 警告 : Task "taskname" not found. 在运行 grunt 时使用 --force 继续

javascript - 使用 Chokidar 监视特定的文件扩展名

javascript - 将 ScrollMagic 安装到 Sails.js 生成的 Web 应用程序中

node.js - Sails Js、Node Js 方法

node.js - 在生产模式下运行 Sails 应用程序需要更长的时间来加载

javascript - 有没有更好的方法在 Sails JS 中使用嵌套(关联)模型?

node.js - 回送在 modelName.afterRemoteError 上给出未定义

javascript - 选择带有纯 js 与 C++ 附加实现的 Node.js 库

node.js - 如何在 ExcelJS 中使用 Promise

javascript - 填充条件失败