저는이 자바 스크립트를 처음 접했고 몽구스 'find'스코프에 대해 약간의 의구심을 가지고 있습니다.몽구스 findOne 범위 의심
나는이 문제를 이해하려고 아래 코드를 작성했습니다.
아래 코드는 쇼핑 컬렉션을 검색 한 다음이 쇼핑에 할당 된 상점을 검색합니다.
storeMap는에 storeId => storeObject의 해시 맵이지만, Store.findOne 범위 끝, storeMap는
var storeMap = {};
Shopping.findOne({ name: shoppingName }, function(err, shopping){
shopping.stores.forEach(function(storeId) {
Store.findOne({_id: storeId}, function(err, store) {
if(err) console.log(err);
console.log(store); //prints out store data
storeMap[storeId] = store;
console.log(storeMap); //prints out store data
});
console.log(storeMap); //prints out an empty array
});
});
그럼, 왜 내 storeMap 배열이 인쇄되어 ... 빈 배열로 롤백 보인다 때 저장소 배열 대신 빈 배열?