设置与迁移的关联后,吸气器不存在

时间:2019-03-25 22:48:40

标签: node.js sequelize.js

我正在尝试使用续集设置1:n关联。当我尝试访问生成的getter函数时,出现错误,提示它们不存在。

如果有两个具有1:n关联的对象,我将按照以下方式定义模型

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Locations = sequelize.define('Locations', {
    owner: DataTypes.INTEGER,
    latitude: DataTypes.DOUBLE,
    longitude: DataTypes.DOUBLE
  }, {});

  Locations.associate = function(models) {
    // associations can be defined here
    Locations.hasMany(models.LocationOpeningHours, {
      as: 'locationOpeningHours',
      foreignKey: 'locationId',
      sourceKey: 'id',
    })
  };
  return Locations;
};

'use strict';
module.exports = (sequelize, DataTypes) => {
  const LocationOpeningHours = sequelize.define('LocationOpeningHours', {
    locationId: DataTypes.INTEGER,
    weekday: DataTypes.TINYINT,
    hours: DataTypes.STRING
  }, {});
  LocationOpeningHours.associate = function(models) {
    // associations can be defined here
    LocationOpeningHours.belongsTo(models.Locations,
        {
          as: 'locations',
          foreignKey: 'id',
          sourceKey: 'locationId',
        })
  };
  return LocationOpeningHours;
};

您期望发生什么?

 locations.findOne({where: {id: locationId}})
     .then(location => {
          location.getLocationOpeningHours()
              .then(openingHours => log(openingHours))

}

我希望看到该位置的locationOpeningHours

实际上发生了什么?

我得到了错误:

TypeError: location.getLocationOpeningHours is not a function
    at locations.findOne.then.location (/home/patric/WebstormProjects/beercoin-backend-nodejs/db/models/locationsmodel.js:765:26)
    at tryCatcher (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/promise.js:694:18)
    at _drainQueueStep (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues (/home/patric/WebstormProjects/beercoin-backend-nodejs/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)

希望任何人都可以指出正确的方向

0 个答案:

没有答案