Here I use a global variable, usergb, because at the "then" block where I get the cart, I have no more access to the user. How can I pass the user with the cart, instead of having to create a global usergb ?
var usergb;
sequelize
.sync()
.then(result => {
return User.findByPk(1);
})
.then(user => {
if (!user) {
return User.create({ name: 'Max', email: 'test@test.com' });
}
return user;
})
.then(user => {
usergb = user;
return user.getCart();
})
.then(cart => { // here I lost the reference to user
if (!cart) {
return usergb.createCart();
}
return cart
})
.then(cart => {
app.listen(3000);
})
.catch(err => {
console.log(err);
});