many to many association in node js

爷,独闯天下 提交于 2020-01-26 04:51:25

问题


here is link of my code . I am getting error in index.js in api-routes-index.js. getting error undefined map function


回答1:


I guess here you have an error:

subcategories: products.subcategories.map(Subcategory => {

products is array, you can't get subcategories from array. Change it to this code:

subcategories: Product.subcategories.map(Subcategory => {




回答2:


Many to many associations in sequilize.js This will be helpful for you to solve the issue:

One product has many order

One order has many product

As a result of many to many we have junction table "orderproduct" which contains product_id and order_id.

   //  In product model
    product.belongsToMany(order, {          
                through: 'orderproduct',
                foreignKey: 'product_id'
            });

   // In order model
    order.belongsToMany(product, {          
                through: 'orderproduct',
                foreignKey: 'order_id'
            });

   // In oderproduct model 
        orderproduct.belongsTo(product, {
            foreignKey: { name: 'product_id', allowNull: false }
        });

        orderproduct.belongsTo (order, {
            foreignKey: { name: 'order_id', allowNull: false }
        });
    }


来源:https://stackoverflow.com/questions/58484484/many-to-many-association-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!