I am building a nodeJS, hapiJS and Sequelize (with PostgreSQL) application and have some issues when I'm using a for loop inside a promise chained in a transaction with Sequelize.
I would like to query my database with different content inside a contents list.
I have founded some solutions with Promises.all() or Promise.map() but I cannot apply this to my problem.
Can anyone explain me the right direction?
db.User.sequelize.transaction(function(t){
return db.User.findOrCreate({
where: {
name: request.payload.user.id
},
transaction: t
})
.then( userResult => {
return db.Order.findOrCreate({
where: {
userId : userResult[0].dataValues.userId,
code : request.payload.order.code,
brand : request.payload.order.brand,
date : request.payload.date
},
transaction: t
})
})
// ############## HERE ##################
.then( orderResult => {
console.log("I WILL BE PRINTED")
for (var i = 0; i < request.payload.content; i++){
console.log("I WILL NOT BE PRINTED")
db.Product.findOrCreate({
where :{ean : request.payload.content[i].product.ean},
transaction: t // FAIRE BOUCLE CONTENT
})
.then( productResult => {
return db.Info.findOrCreate({
where: {
//TODO : prefix et password
productId: productResult[0].dataValues.productId,
orderId: orderResult[0].dataValues.orderId
}
}).then( infoResult => {
console.log(productResult)
return db.Range.create({
infoId : infoResult[0].dataValues.infoId,
low : productResult[0].dataValues.offset,
high : request.payload.content[i].quantity
})
})
})
}
})
.then(res => { return console.log("ok")})
})