node.js - sequelize belongsToMany 给出 TypeError : undefined is not a function

标签 node.js express sequelize.js

我正在使用 sequelize 和 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 .

关于node.js - sequelize belongsToMany 给出 TypeError : undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005751/

相关文章:

node.js - Safari 和 iOS 中的 Express.js 中的 Gzip 压缩问题

sql - 用 SQL 或应用程序语言定义数据库模式哪个更好?

javascript - 如何在 BreezeJS 元数据中定义矩阵/链接/交叉表连接?

javascript - 如何避免 NodeJs、Express 中的 If else hell

node.js - Node 和 Handlebars 文件夹结构

javascript - 在快速中间件中调用时,这在原型(prototype)函数中不起作用

javascript - isAuthenticated() 函数无法正常工作 Node.js Passport

javascript - 如何在不创建模拟的情况下测试数据库查询

javascript - 使用 Nock.js 模拟类上的 Axios 方法

javascript - 如何按文件/模块分离 JSDoc