1
我試圖通過MongoDB數據庫與貓鼬重定向到Emberjs前端來達到另一個選擇。在Mongoose中發出多個請求
如果上面的文字不清晰,看數據庫的模式:
// I already get the exam content given an id
Exam:{
...
collections:[{
question: Object(Id)
}]
...
}
,並在問題的模式是:
// I want to get content of this giving an id
question:{
...
questions: String, // I want to get this given an Id exam
value: Number // and this
...
}
我試圖把它得到的對象ID然後做一個提取每個問題,並將返回值保存到像這樣的json變量中:
Test.findById(id, 'collections', function(err, collections){
if(err)
{
res.send(err);
}
var result ={}; //json variable for the response
// the for it's with the number of objectId's that had been found
for(var i = 0; i < collections.collections.length; i++)
{
// Send the request's to the database
QuestionGroup.findById(collections.collections[i].id, 'questions').exec(function(err, questiongroup)
{
if(err)
{
res.send(err);
}
// save the results in the json
result.questiongroup = questiongroup;
// prints questions
console.log(result);
});
// it return's {}
console.log(result);
}
// also return {}
console.log(result);
res.json({result: result});
});
有沒有辦法將請求保存到變量中,並將其像json一樣返回到前端?
非常感謝你,我dind't看到,你可以先填充數組,然後發送一個響應,它的工作守則,並且知道我可以更深入地瞭解我的解決方案,謝謝。 –