Issue:
The createCart()
function in app.js
is allowing multiple carts to be created for a single user when the server is reloaded.
Expected Behavior:
createCart()
should only create a single cart per user, regardless of how many times the server is reloaded.
Fenced Code Blocks:
Product.belongsTo(User, { constraints: true, onDelete: 'CASCADE' });
User.hasMany(Product);
User.hasOne(Cart);
Cart.belongsTo(User);
Cart.belongsToMany(Product, { through: CartItem });
Product.belongsToMany(Cart, { through: CartItem });
sequelize
// .sync({ force: true })
.sync()
.then(result => {
return User.findByPk(1);
// console.log(result);
})
.then(user => {
if (!user) {
return User.create({ name: 'Max', email: 'test@test.com' });
}
return user;
})
.then(user => {
// console.log(user);
return user.createCart();
})
.then(cart => {
app.listen(4000);
})
.catch(err => {
console.log(err);
});