sequelize belongsToMany giving TypeError: undefined is not a function
sequelize belongsToMany giving TypeError: undefined is not a function
我正在使用 sequelize with express,我已经这样定义了我的模型
"use strict";
var Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
var Order = sequelize.define("Order",
{
status: {
type: Sequelize.INTEGER
},
notes: {
type: Sequelize.STRING
}
},
{
classMethods: {
associate: function(models) {
Order.belongsTo(models.User);
Order.belongsTo(models.Address);
Order.belongsToMany(models.Items);
}
}
}
);
return Order;
}
我在尝试 运行 我的应用程序时收到以下错误消息
Order.belongsToMany(models.Items );
^
TypeError: undefined is not a function
at sequelize.define.classMethods.associate (models/order.js:18:7)
at models/index.js:24:19
at Array.forEach (native)
at Object. (models/index.js:22:17)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
我的 index.js 文件与此处给出的示例相同:https://github.com/sequelize/express-example/blob/master/models/index.js
我的物品模型是这样的:
"use strict";
var Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
var Item = sequelize.define("Item", {
title: {
type: Sequelize.STRING
},
mrp: {
type: Sequelize.DECIMAL,
allowNull: false
},
sp: {
type: Sequelize.DECIMAL,
allowNull: false
},
desc: {
type: Sequelize.STRING
},
skuId: {
type: Sequelize.STRING,
allowNull: false
},
type: {
type: Sequelize.STRING
},
merchantId: {
type: Sequelize.STRING,
allowNull: false
},
categoryId: {
type: Sequelize.STRING,
allowNull: false
}
}
);
return Item;
}
我也试过在 app.js 中给 belongsToMany 但它不起作用。所有其他协会都正常工作。
请帮忙。
您在 order.js 中输入错误,您想要 Item
而不是 Items
。像这样:
Order.belongsToMany(models.Item);
此外,您可能会收到一条已弃用的消息,要求您为此 belongsToMany
定义 through
参数,例如:
Order.belongsToMany(models.Item, {
through: 'OrderItems'
});
最后,belongsToMany
在sequelize 2.1.0版本中引入,it seems。
我正在使用 sequelize with express,我已经这样定义了我的模型
"use strict";
var Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
var Order = sequelize.define("Order",
{
status: {
type: Sequelize.INTEGER
},
notes: {
type: Sequelize.STRING
}
},
{
classMethods: {
associate: function(models) {
Order.belongsTo(models.User);
Order.belongsTo(models.Address);
Order.belongsToMany(models.Items);
}
}
}
);
return Order;
}
我在尝试 运行 我的应用程序时收到以下错误消息
Order.belongsToMany(models.Items );
^
TypeError: undefined is not a function
at sequelize.define.classMethods.associate (models/order.js:18:7)
at models/index.js:24:19
at Array.forEach (native)
at Object. (models/index.js:22:17)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
我的 index.js 文件与此处给出的示例相同:https://github.com/sequelize/express-example/blob/master/models/index.js
我的物品模型是这样的:
"use strict";
var Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
var Item = sequelize.define("Item", {
title: {
type: Sequelize.STRING
},
mrp: {
type: Sequelize.DECIMAL,
allowNull: false
},
sp: {
type: Sequelize.DECIMAL,
allowNull: false
},
desc: {
type: Sequelize.STRING
},
skuId: {
type: Sequelize.STRING,
allowNull: false
},
type: {
type: Sequelize.STRING
},
merchantId: {
type: Sequelize.STRING,
allowNull: false
},
categoryId: {
type: Sequelize.STRING,
allowNull: false
}
}
);
return Item;
}
我也试过在 app.js 中给 belongsToMany 但它不起作用。所有其他协会都正常工作。 请帮忙。
您在 order.js 中输入错误,您想要 Item
而不是 Items
。像这样:
Order.belongsToMany(models.Item);
此外,您可能会收到一条已弃用的消息,要求您为此 belongsToMany
定义 through
参数,例如:
Order.belongsToMany(models.Item, {
through: 'OrderItems'
});
最后,belongsToMany
在sequelize 2.1.0版本中引入,it seems。