[英]How to handle the inner query in nodejs?
Created an Rest Post-type API in nodeJS,in which : I am executing two queries here. 在nodeJS中创建了一个Rest Post类型的API,其中:我在这里执行两个查询。 1. Firstly Executing the query on answers table to fetch user-Id and answer detail as well in that table.
1.首先在答案表上执行查询,以获取该表中的用户ID和答案详细信息。 // i have check in the my console they show me two user-Id 2.Second ,executing the query on users table to fetch users detail on the basis of user-Id that i pass in find function.
//我已经在我的控制台中检查了它们向我显示的两个用户ID。第二,对用户表执行查询以根据我传入的find函数的用户ID获取用户详细信息。 // check my console they show me two answer object because i have two user-Id.
//检查我的控制台,因为我有两个用户ID,所以它们向我显示了两个答案对象。 ------------------ above process work fine --------------------- Now, i got stuck because , i have two merge both result into one object.
------------------上面的程序工作正常---------------------现在,我被卡住了,因为我有两个合并两个结果到一个对象。 I'm doing this but isn't work perfect.Help me out here..!!
我正在执行此操作,但效果并不理想。请在这里帮助我!
My code : - 我的代码:-
app.post('/getTopAns', function(req, res) {
console.log("inside getTopAns ");
var questionId=req.body.questionId;
mongoose.model('answers').find({
questionId:questionId,
compliance:"Y"
}, function(err, ansResult){
for (var i = 0;i<ansResult.length;i++) {
mongoose.model('users').findOne({
userId:ansResult[i].userId,
}, function(err,usrResult){
var obj = {
followerLength : usrResult.follower.length,
upvote : ansResult[i].upvote
}
})
console.log(obj);
}
});
})
Maybe you should first log ansResult to see if db is returning this. 也许您应该首先登录ansResult来查看db是否正在返回此结果。 If it is, then check usrResult.
如果是,则检查usrResult。 If both are returned, I usually use lodash's assign or merge to merge two objects into one
如果两个都返回,我通常使用lodash的assign或merge将两个对象合并为一个
I recommend using promises instead of callback to handle async easier with node.js. 我建议使用promise而不是回调,以便通过node.js更轻松地处理异步。 The flow is to get the first call result, map the array of result into a list of promises which we can wait for the result by using
Promise.all
. 流程是获取第一个调用结果,将结果数组映射到
Promise.all
列表中,我们可以使用Promise.all
等待结果。 Finally map the second results which the first list. 最后将第二个结果映射到第一个列表中。
app.post("/getTopAns", function(req, res) {
console.log("inside getTopAns ");
var questionId = req.body.questionId;
mongoose.model("answers")
.find({
questionId: questionId,
compliance: "Y"
})
.then(ansResult => {
const promises = ansResult.map(ans => {
return mongoose.model("users")
.findOne({ userId: ansResult[i].userId })
})
return Promise.all(promises)
.then(usrResults => {
return usrResults.map((usrResult, i) => {
followerLength: usrResult.follower.length,
upvote: ansResult[i].upvote
})
})
})
.then(results => {
console.log('got all results here', results)
})
});
It can be even better using async/await
. 使用
async/await
可能会更好。
app.post("/getTopAns", async (req, res) => {
console.log("inside getTopAns ");
var questionId = req.body.questionId;
const ansResult = mongoose.model("answers")
.find({
questionId: questionId,
compliance: "Y"
})
const promises = ansResult.map(ans => {
return mongoose.model("users")
.findOne({ userId: ansResult[i].userId })
})
const usrResults = await Promise.all(promises)
const results = usrResults.map((usrResult, i) => {
followerLength: usrResult.follower.length,
upvote: ansResult[i].upvote
})
console.log('got all results here', results)
});
More information can be found here: https://medium.com/@ThatGuyTinus/callbacks-vs-promises-vs-async-await-f65ed7c2b9b4 可以在这里找到更多信息: https : //medium.com/@ThatGuyTinus/callbacks-vs-promises-vs-async-await-f65ed7c2b9b4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.