如何在 Sequelize 中使用 belongsToMany 的 add 方法?
How to use add Methods with belongsToMany in Sequelize?
我正在尝试将产品添加到我的购物车商品中 Table
使用 sequilize:
Product.belongsTo(用户, { constraints: true, onDelete: 'CASCADE' });
User.hasMany(产品);
User.hasOne(购物车);
Cart.belongsTo(用户);
Cart.belongsTo许多(产品,{通过:CartItem});
Product.belongsTo许多(购物车,{通过:CartItem});
这是我的产品型号:
const Product = sequelize.define('product', {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
title: {
type: Sequelize.STRING,
allowNull: false,
},
price: {
type: Sequelize.INTEGER,
allowNull: false,
},
imageURL: {
type: Sequelize.STRING,
allowNull: false,
},
description: {
type: Sequelize.STRING,
allowNull: false,
},
});
module.exports = Product;
这是我的购物车型号:
const Cart = sequelize.define('cart', {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
});
module.exports = Cart;
这是我的用户模型:
const User = sequelize.define('user', {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
fullName: {
type: Sequelize.STRING,
allowNull: false,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
});
module.exports = User;
这是我的购物车商品型号:
const CarteItem = sequelize.define('carteItem', {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
quantity: {
type: Sequelize.INTEGER,
allowNull: false,
},
});
module.exports = CarteItem;
当我使用
Post 将产品添加到购物车时出现问题
exports.postCart = (req, res, next) => {
const prodId = req.body.productId;
let fetchedCart;
let newQuantity = 1;
req.user
.getCart()
.then((cart) => {
fetchedCart = cart;
return cart.getProducts({ where: { id: prodId } });
})
.then((products) => {
let product;
if (products > 0) product = products[0];
return Product.findByPk(prodId);
})
.then((product) => {
console.log(product);
return fetchedCart.addProduct(product, {
through: { quantity: newQuantity },
});
})
.then(() => {
res.redirect('/');
})
.catch((err) => console.log(err));
};
我收到这个错误
Executing (default): SELECT `id`, `quantity`, `createdAt`, `updatedAt`, `cartId`, `productId` FROM `carteItems` AS `carteItem` WHERE `carteItem`.`cartId` = '1' AND `carteItem`.`productId` IN ('7c64a81f-59d3-427d-af2a-f9c9ee6e4fde');
AggregateError
at recursiveBulkCreate (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2600:17)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Function.bulkCreate (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2837:12)
at async Promise.all (index 0)
at async BelongsToMany.add (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/associations/belongs-to-many.js:740:30) {
errors: [
BulkRecordError [SequelizeBulkRecordError]: notNull Violation: carteItem.id cannot be null
at /home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2594:25
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Promise.all (index 0)
at async recursiveBulkCreate (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2590:9)
at async Function.bulkCreate (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2837:12)
at async Promise.all (index 0)
at async BelongsToMany.add (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/associations/belongs-to-many.js:740:30) {
errors: [ValidationError],
record: [carteItem]
}
]
}
我的问题是使用了 CartItem Schema 的错误定义,应该是 autoIncremented
我正在尝试将产品添加到我的购物车商品中 Table
使用 sequilize:
Product.belongsTo(用户, { constraints: true, onDelete: 'CASCADE' });
User.hasMany(产品);
User.hasOne(购物车);
Cart.belongsTo(用户);
Cart.belongsTo许多(产品,{通过:CartItem});
Product.belongsTo许多(购物车,{通过:CartItem});
这是我的产品型号:
const Product = sequelize.define('product', {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
title: {
type: Sequelize.STRING,
allowNull: false,
},
price: {
type: Sequelize.INTEGER,
allowNull: false,
},
imageURL: {
type: Sequelize.STRING,
allowNull: false,
},
description: {
type: Sequelize.STRING,
allowNull: false,
},
});
module.exports = Product;
这是我的购物车型号:
const Cart = sequelize.define('cart', {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
});
module.exports = Cart;
这是我的用户模型:
const User = sequelize.define('user', {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
fullName: {
type: Sequelize.STRING,
allowNull: false,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
});
module.exports = User;
这是我的购物车商品型号:
const CarteItem = sequelize.define('carteItem', {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
quantity: {
type: Sequelize.INTEGER,
allowNull: false,
},
});
module.exports = CarteItem;
当我使用
Post 将产品添加到购物车时出现问题exports.postCart = (req, res, next) => {
const prodId = req.body.productId;
let fetchedCart;
let newQuantity = 1;
req.user
.getCart()
.then((cart) => {
fetchedCart = cart;
return cart.getProducts({ where: { id: prodId } });
})
.then((products) => {
let product;
if (products > 0) product = products[0];
return Product.findByPk(prodId);
})
.then((product) => {
console.log(product);
return fetchedCart.addProduct(product, {
through: { quantity: newQuantity },
});
})
.then(() => {
res.redirect('/');
})
.catch((err) => console.log(err));
};
我收到这个错误
Executing (default): SELECT `id`, `quantity`, `createdAt`, `updatedAt`, `cartId`, `productId` FROM `carteItems` AS `carteItem` WHERE `carteItem`.`cartId` = '1' AND `carteItem`.`productId` IN ('7c64a81f-59d3-427d-af2a-f9c9ee6e4fde');
AggregateError
at recursiveBulkCreate (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2600:17)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Function.bulkCreate (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2837:12)
at async Promise.all (index 0)
at async BelongsToMany.add (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/associations/belongs-to-many.js:740:30) {
errors: [
BulkRecordError [SequelizeBulkRecordError]: notNull Violation: carteItem.id cannot be null
at /home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2594:25
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Promise.all (index 0)
at async recursiveBulkCreate (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2590:9)
at async Function.bulkCreate (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/model.js:2837:12)
at async Promise.all (index 0)
at async BelongsToMany.add (/home/horus/Documents/WorkStation/learningPath/learningPathNode/node-complete-course/node_modules/sequelize/lib/associations/belongs-to-many.js:740:30) {
errors: [ValidationError],
record: [carteItem]
}
]
}
我的问题是使用了 CartItem Schema 的错误定义,应该是 autoIncremented