TypeError:无法读取未定义风帆的属性“集合”

时间:2017-08-06 16:21:56

标签: node.js sails.js tedious

我正在使用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()并且仍然出现同样的错误。

1 个答案:

答案 0 :(得分:0)

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

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

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