1

我使用sails-sqlserver 作为我的适配器我只是尝试在以下模型之一的数据库中创建一个新行。

这是第一个模型:

// Roles.js

module.exports = {

  autoCreatedAt: false,
  autoUpdatedAt: false,

  attributes: {

    name: {
      type: 'string'
    },

    approval_level: {
      model: 'approval_levels'
    },

    specified: {
      type: 'boolean' 
    },                
    entity_operations: {
      collection: 'entity_operations',
      via: 'roles',
      dominant: true
    },

    users: {
      collection: 'system_users',
      via: 'role'
    }

  },

  createRole: function (name, approval_level, cb) {

    values = {
      name: name,
      approval_level: approval_level
    };

    Roles.create(values).exec(cb);
  },


  getAll: function (cb) {
    Roles.find().exec(cb);
  }


};

这是第二个模型:

// Entity_Operations.js

module.exports = {

    autoCreatedAt: false,
    autoUpdatedAt: false,

    attributes: {

        entity_name: {
            type: 'string'
        },

        operation: {
            model: 'operations'
        },

        roles: {
            collection: 'roles',
            via: 'entity_operations'
        }

    },

    getAll: function (cb) {
        Entity_operations.find().exec(cb);
    }

};

这两个模型有一个多对多的关系,我想做的就是这样:

Entity_operations.create({
            entity_name: 'example',
            operation: 6
        }).exec((err, entity: Entity_operations) => {
            console.log(entity);
        });

那么这个错误就出来了,没有解释任何可以帮助我知道这个错误来自哪里的东西:

 /opt/nodejs/back-heaven/dev/node_modules/sails-sqlserver/lib/adapter.js:435
      Object.keys(connections[connection].collections[collection].definition).forEach(function(key) {
                                         ^
TypeError: Cannot read property 'collections' of undefined
    at Object.getPrimaryKey (/opt/nodejs/back-heaven/dev/node_modules/sails-sqlserver/lib/adapter.js:435:42)
    at Object.create (/opt/nodejs/back-heaven/dev/node_modules/sails-sqlserver/lib/adapter.js:374:24)
    at module.exports.create (/opt/nodejs/back-heaven/dev/node_modules/waterline/lib/waterline/adapter/dql.js:84:13)
    at child.createValues (/opt/nodejs/back-heaven/dev/node_modules/waterline/lib/waterline/query/dql/create.js:220:16)
    at /opt/nodejs/back-heaven/dev/node_modules/waterline/lib/waterline/query/dql/create.js:74:20
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:726:13
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:52:16
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:269:32
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:44:16
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:723:17
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:167:37
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:52:16
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:269:32
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:44:16
    at child.<anonymous> (/opt/nodejs/back-heaven/dev/node_modules/waterline/lib/waterline/utils/schema.js:152:44)
    at fn (/opt/nodejs/back-heaven/dev/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:41:10)
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:181:20
    at iterate (/opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:262:13)
    at Object.async.forEachOfSeries.async.eachOfSeries (/opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:281:9)
    at Object.async.forEachSeries.async.eachSeries (/opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:214:22)
    at Object.runner.beforeCreate (/opt/nodejs/back-heaven/dev/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:44:9)
    at /opt/nodejs/back-heaven/dev/node_modules/waterline/lib/waterline/query/dql/create.js:180:17
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:718:13
    at iterate (/opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:262:13)
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:274:29
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:44:16
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:723:17
    at /opt/nodejs/back-heaven/dev/node_modules/async/lib/async.js:167:37

当我在控制器中尝试相同的代码时,它成功地在我自己的实现的播种机中尝试它,这个播种机在它开始之前我以编程方式制作一个sails对象并调用sails.load

像这样 :

let myApp = new Sails();

    myApp.load({}, (err) => {
      if (err) throw err;

      // this execute the seeds in the database seeder class
      let seeder = require(`../../api/seeders/${scope.args[0]}`);
      seeder.handle(() => {

        myApp.lower((err) => {
          if (err) throw err;
          console.log(`Seeder ` + scope.args[0] + ` finished seeding`);
        });

      });

    });

我也尝试过sails.lift() 仍然是同样的错误。

4

1 回答 1

0

我发现了问题,我只是在制作一个回调函数,它应该调用sails.lower(),它卸载或关闭帆我把它放在错误的地方,甚至在模型创建代码开始之前就调用它。忘了在数据库回调函数中调用它。

所以,对于其他在风帆中遇到同样问题的人来说,你的问题是风帆实际上没有加载,或者当你正在处理的操作开始风帆工作时,但之后由于某种原因风帆停止了这使得模型这样做很奇怪寻找问题我希望sails在他们的代码中处理这样的错误,以显示出更具表现力的错误信息。

我希望这对任何人都有帮助。

于 2017-08-07T23:23:39.357 回答