【发布时间】:2018-09-21 13:27:00
【问题描述】:
我正在构建一个 nodeJS、hapiJS 和 Sequelize(使用 PostgreSQL)应用程序,当我在与 Sequelize 的事务中链接的 Promise 中使用 for 循环时遇到一些问题。
我想用内容列表中的不同内容查询我的数据库。
我已经使用 Promises.all() 或 Promise.map() 建立了一些解决方案,但我无法将其应用于我的问题。
谁能解释我正确的方向?
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")})
})
【问题讨论】:
-
如果循环内的
console.log没有显示出来,那么问题在于循环的条件。但是您需要使用Promise.all或Promise.map来解决它。为什么您认为它们不能应用于您的问题? -
@t.niese 我的意思是我没有成功应用它们
标签: javascript node.js promise sequelize.js