node.js - 无法读取未定义的属性 'menu_id'

标签 node.js sequelize.js relationship

我尝试用 3 个表创建数据库: restaurantrestaurant_menumenu ,它们的关系是餐厅有很多菜单,菜单可以通过 Nodejs 中的 Sequelize 属于许多餐厅。

餐厅模型.js

'use strict';
module.exports = (sequelize, DataTypes) => {
    const restaurant = sequelize.define('restaurant', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            autoIncrement: true,
            primaryKey: true
        },
        name: DataTypes.STRING,
        address: DataTypes.STRING,
        phone: DataTypes.STRING,
        lat: {
            type: DataTypes.DOUBLE,
            allowNull: false,
            defaultValue: 0
        },
        lng: {
            type: DataTypes.DOUBLE,
            allowNull: false,
            defaultValue: 0
        },
        user_owner: {
            type: DataTypes.INTEGER,
            defaultValue: 0
        },
        image: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        payment_url: {
            type: DataTypes.TEXT,
            allowNull: false
        }
    }, {
        freezeTableName: true,
        timestamps: false
    });
    restaurant.associate = function (models) {
        // associations can be defined here
        restaurant.belongsToMany(models.menu, {
            through: {
              model: models.restaurant_menu
            },
            foreignKey: 'restaurant_id'
        })
    };
    return restaurant;
};

菜单.model.js
'use strict';
module.exports = (sequelize, DataTypes) => {
    const menu = sequelize.define('menu', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            allowNull: false
        },
        name: DataTypes.STRING(50),
        description: DataTypes.STRING(500),
        image: DataTypes.TEXT
    }, {
        freezeTableName: true,
        timestamps: false
    });
    menu.associate = function (models) {
        // associations can be defined here
        menu.belongsToMany(models.restaurant, {
            through: {
                model: models.restaurant_menu
            },
            foreignKey: "menu_id"
        });
    };
    return menu;
};

restaurant_menu.model.js
'use strict';
module.exports = (sequelize, DataTypes) => {
    const restarant_menu = sequelize.define('restarant_menu', {
        restaurant_id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            allowNull: false,
            references: {
                model: 'restaurant'
            }
        },
        menu_id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            allowNull: false,
            references: {
                model: 'menu'
            }
        }
    }, {
        freezeTableName: true,
        timestamps: false
    });
    restarant_menu.associate = function (models) {
        // associations can be defined here
    };
    return restarant_menu;
};

我尝试运行迁移,但出现错误:
Cannot read property 'menu_id' of undefined

我该如何解决?

最佳答案

我相信您正在编写旧语法,结帐文档。

https://sequelize.org/master/manual/advanced-many-to-many.html

关于node.js - 无法读取未定义的属性 'menu_id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61266889/

相关文章:

Node.JS HTML 到 PDF

null - express + express-graphql helloworld 返回 null

mysql - 如何在Sequelize中配置一对多关系?

json - jackson /GSON : Selecting a different representation when object has been reached via a relationship

javascript - 用于具有关系字段的内容类型的 Strapi POST API

java - 确认此继承有效

javascript - CRC-16-IBM 实现 (JS) 不工作

node.js - 尝试将 Electron 更新程序添加到 Electron 项目时出现错误 TS2305 : Module '"http "' has no exported member ' OutgoingHttpHeaders'.

javascript - 我可以在 Sublime 2 中调试 node.js 应用程序吗?

javascript - Sequelize 事务方法未被调用