【问题标题】:How to run a command on startup of sails.js如何在sails.js 启动时运行命令
【发布时间】:2014-04-18 21:06:32
【问题描述】:

如何在sails.js 上运行命令?我正在尝试在执行任何路由或控制器之前清除集合。但是,在加载模型之后。

在 express 中很简单,你可以将它放在 apps.js 中。在sails.js 中找不到它的执行位置

示例代码:

User.update({}, {$set : {'socket': [] } }, {'multi': true}, function(err){ if (err){ console.log(err)} });

【问题讨论】:

  • 仅供参考,您发布的查询不会按预期工作,因为它使用的是 Waterline ORM 无法理解的本机 MongoDB 语法。就做User.update({}, {'socket': []}).exec(function(err, users){...})

标签: mongodb sails.js


【解决方案1】:

您可以将启动逻辑放在/config/bootstrap.js 中。只要记得在最后调用回调函数,让 Sails 继续升起!

【讨论】:

    【解决方案2】:

    这是/config/bootstrap.js 中的一个示例,它在没有用户时设置了一些用户:

    module.exports.bootstrap = async function(done) {
      // Set up administrators when there is no users
      if ((await Users.count()) === 0) {
        await Users.createEach([
          { email: "julien@gmail.com", password: "changemeasap" },
          { email: "nada@gmail.com", password: "changemeasap" }
        ]);
      }
    
      // Don't forget to trigger `done()` when this bootstrap function's logic is finished.
      // (otherwise your server will never lift, since it's waiting on the bootstrap)
      return done();
    };
    

    【讨论】:

      猜你喜欢
      • 2013-05-25
      • 1970-01-01
      • 2017-05-29
      • 2010-10-09
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多