2015-05-02 132 views
3

我使用与快递sequelize,我定义我的模型是这样sequelize belongsToMany给类型错误:未定义是不是一个函数

"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但它不起作用。所有其他协会都正常工作。 请帮忙。

+0

你使用的是什么版本的sequelize?你怎么打电话给'associate'? –

+0

版本为[email protected],关联从index.js调用,与https://github.com/sequelize/express-example/blob/master/models/index.js – akshay202

+0

相同嗯,订单。 js对我来说看起来很好。你可以发布你的代码“Items”吗? –

回答

2

你在order.js有一个错字,你想Item不是Items。就像这样:

Order.belongsToMany(models.Item); 

此外,你可能会被弃用的消息,要求您定义through参数有关此belongsToMany,比如像这样:

Order.belongsToMany(models.Item, { 
    through: 'OrderItems' 
}); 

最后,belongsToMany在sequelize版本2.1中引入的。 0,it seems

+0

还是同样的错误:(..我不知道为什么只,而所有其他协会正在belongsToMany不起作用。 – akshay202

+1

其实我已经克隆了回购,并得到您的代码在我的工作进行到底我不确定该说什么,你可以尝试附加一个调试器(例如'node-inspector')来确认报告的错误的行号是否正确,并且验证“Order”和“Order”。 belongsToMany'被定义为预期的。 –

+0

还要注意在示例中有一个'Task.belongsToMany(models.User);'似乎可以工作,我建议也试着理解你的'Order'和这个示例的'Task'模型 –

相关问题