I have the below sequence of the chained loop which I want to return using promises but I get the response before the forEach is executed in my code... Can anyone tell me where I am going wrong... I would like to do it using native Promises and would not prefer to use await/async so I get a better understanding of how Promises function.
Details.find(function(err,details){
if(err){
res.send(err)
}else{
console.log("----------First Promise------------")
return details
}
}).then(result1 => {
result1.forEach(function(item){
renderData = {}
OrgChart.findOne({GID:item.gid},function(err,detail){
console.log("Detail is ------> " + detail.DISPLAY_NAME)
if(err){
res.send(err)
}else{
return detail.DISPLAY_NAME
}
}).then( result2 => {
renderData.gid = result2.DISPLAY_NAME
renderData.pageouttime = item.pageouttime
renderData.createdAt = item.createdAt
renderData.oncall = item.oncall
renderData.comments = item.comments
renderData.actionLink = item._id.toString()
console.log(renderData)
dataArr.push(renderData)
})
})
}).then(result3 => {
console.log("Final Result is ----> " + result3)
response.data = result3
res.json(response)
})
Inside the forEach, I want to get a value using a value of the row. I am new to node js and Promises... I want to achieve something like below but using Promises.
Example sequence I want to Achieve through Promises
var someArray = []
var DetailsObj = Details.find()
DetailsObj.each(function(item){
var newMap = {}
newMap.prop1=item.prop1
newMap.prop2 = item.prop2
newMap.prop3 = OrgChart.find({id:item.prop3}).displayName
someArray.push(newMap)
})
Please, can anyone let me know where I am going wrong?
Update 1(Not Working)
return Promise.all(result1.map(function(item){
renderData = {}
OrgChart.findOne({GID:item.gid},function(err,detail){
console.log("Detail is ------> " + detail.DISPLAY_NAME)
if(err){
res.send(err)
}else{
return detail.DISPLAY_NAME
}
}).then(result2 => {
renderData.gid = result2.DISPLAY_NAME
renderData.pageouttime = item.pageouttime
renderData.createdAt = item.createdAt
renderData.oncall = item.oncall
renderData.comments = item.comments
renderData.actionLink = item._id.toString()
console.log(renderData)
dataArr.push(renderData)
})
}))
Still i get an empty array
Update 2 (Added return from the map callback--- Still not Working)
return Promise.all(result1.map(function(item){
renderData = {}
OrgChart.findOne({GID:item.gid},function(err,detail){
console.log("Detail is ------> " + detail.DISPLAY_NAME)
if(err){
res.send(err)
}else{
return detail.DISPLAY_NAME
}
}).then(result2 => {
renderData.gid = result2.DISPLAY_NAME
renderData.pageouttime = item.pageouttime
renderData.createdAt = item.createdAt
renderData.oncall = item.oncall
renderData.comments = item.comments
renderData.actionLink = item._id.toString()
console.log(renderData)
dataArr.push(renderData)
})
return dataArr
})
)
Update 3(Updated after returning from the then() callback in the Promise.all() block --- Still not Working)
return Promise.all(result1.map(function(item){
renderData = {}
OrgChart.findOne({GID:item.gid},function(err,detail){
console.log("Detail is ------> " + detail.DISPLAY_NAME)
if(err){
res.send(err)
}else{
return detail.DISPLAY_NAME
}
}).exec().then(result2 => {
renderData.gid = result2.DISPLAY_NAME
renderData.pageouttime = item.pageouttime
renderData.createdAt = item.createdAt
renderData.oncall = item.oncall
renderData.comments = item.comments
renderData.actionLink = item._id.toString()
return renderData
})
})
)
Promise.all(), insted of using foreach, insert your queries into an array, and process it throughPromise.all()developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…returnthe promise from themapcallbacknot workingcan only be answered byyou're doing something wrong. not very helpful for anyone. if something isnot workingthen you usually expect something but something else happens. You should provide this information. Like I expected .... but got error ... or got ... instead.