I have 3 tables, orders, products and the pivot table, order_products, the table orders and products have i relation many to many, but when i try to use the method
order.addProduts()
Don't work, return the error
UnhandledPromiseRejectionWarning: TypeError: Order.addProducts is not a function
But i had verify my model of products and orders, the relation its ok, i don't see anything wrong
my model and my controller when i try to use the method:
class Products extends Model {
static init(sequelize) {
super.init({
name: DataTypes.STRING,
price: DataTypes.FLOAT,
description: DataTypes.STRING
},{
sequelize
})
}
static associate(models){
this.belongsToMany(models.Orders, {as: 'order_product', foreignKey: 'product_id', through: 'order_products'})
}
}
module.exports = Products;
class Orders extends Model {
static init(sequelize) {
super.init({
name: DataTypes.STRING,
street: DataTypes.STRING,
phonenumber: DataTypes.STRING,
number: DataTypes.STRING,
reference: DataTypes.TEXT,
note: DataTypes.TEXT,
}, {
sequelize,
})
}
static associate(models){
this.belongsToMany(models.Products, {as: 'product_order', foreignKey: 'order_id', through: 'order_products'})
}
}
My controller
async store (req, res){
const {name, phonenumber, street, number, reference, note, value_subtotal, value_delivery, value_total, products} = req.body;
if(!(name|| phonenumber || value_subtotal || value_total))return res.status(406).send({message: 'missing data!'})
if((value_total|| value_subtotal) <= 0)return res.status(406).send({message: "total value or subtotal value can't be 0 or less than"})
//console.log(products)
const Order = await Orders.create({
name,
phonenumber,
street,
number,
reference,
note,
value_subtotal,
value_delivery,
value_total
})
await Order.addProducts(products)
return res.status(201).json(Order);
},