2
서로 의존하는 두 개의 데이터베이스 쿼리를 만들어야하는 인스턴스가 많이 있습니다.하지만 js의 비동기 특성으로 인해 이러한 쿼리 요청의 순서를 설정할 수 없습니다 . 내가 사용하고있는 작업은 다른 쿼리의 콜백에 쿼리 중 하나를 넣는 것입니다. 내 접근 방식은 작동하는 것처럼 보이지만이 작업을 수행하는 더 좋은 방법이 있는지 알고 싶습니다.Mongoose에서 Express가있는 두 개의 데이터베이스 쿼리
예제 코드 :
app.get('/tweet', function(req,res){
User.findOne({ twitterID: req.user.id },
function(err, user) {
if (user){
var votedPosts = user.votedPosts;
Post.find(function (err, posts) {
if(posts){
for (var i = 0; i < posts.length; i++){
for (var j = 0; j < votedPosts.length; j++){
if(votedPosts[j]._id == posts[i]._id){
posts[i].votetype = votedPosts[j].votetype;
}
else{
console.log("this is getting called");
posts[i].votetype = 0;
}
}
}
res.send(posts);
}
});
}
});
});
중복 : http://stackoverflow.com/questions/13243507/rendering-results-of-multiple-db-mongoose-queries-to-a-view-in-express-js – verybadalloc