나는 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
동물은 없으며 청과물 만 있습니다. 이 경우에 .. 토마토는 과일과 채소로 간주됩니다. – Rolando
개는 과일입니까, 야채입니까? :-) – numbers1311407