我使用nodejs + express + mongoose。假设我有两个模式和模型:“水果”和“蔬菜”。如何同步请求?
假设我有以下几点:
var testlist = ["Tomato", "Carrot", "Orange"];
var convertedList = [];
// Assume res is the "response" object in express
我婉能够在阵列中检查每个项目对“水果”和“蔬菜”集合分别将它们插入到一个转换的列表,其中番茄,胡萝卜和西兰花被他们各自的文件替换。
下面我有一些我认为会是的伪代码,但不知道该怎么做。
for(var i = 0; i < testlist.length; i++) {
var fruitfind = Fruit.find({"name":testlist[i]});
var vegfind = Vegetables.find({"name":testlist[i]});
// If fruit only
if(fruitfind) {
convertedList.push(fruitfindresults);
}
// If vegetable only
else if(vegfind) {
convertedList.push(vegfindresults);
}
// If identified as a fruit and a vegetable (assume tomato is a doc listed under both fruit and vegetable collections)
else if (fruitfind && vegfind) {
convertedList.push(vegfindresults);
}
}
// Converted List should now contain the appropriate docs found.
res.send(convertedList) // Always appears to return empty array... how to deal with waiting for all the callbacks to finish for the fruitfind and vegfinds?
这样做的最好方法是什么?或者这甚至有可能?
有动物吗?似乎在你的问题上有些混乱。 – numbers1311407 2013-05-06 14:56:30
没有动物,只有水果和蔬菜。注意在这种情况下..番茄被认为是水果和蔬菜。 – Rolando 2013-05-06 14:57:32
狗是水果还是蔬菜? :-) – numbers1311407 2013-05-06 14:58:36