2017-11-22 12 views
1
User.find().then(function (user) { 
    console.log(user) 
}) 
Category.find().then(function (category) { 
    console.log(err); 
}) 
Content.find().then(function (content) { 
    console.log(content); 
}) 

質問文を1つにまとめてすべての結果を得るにはどうすればよいですか? ps:私はmongooseをmongoDbの操作に使用します。複数のマングースを1つに結合する方法

+0

質問文が意味するもの。私たちはfind()内を渡しますか? –

+0

Promise.allを使用してすべてのクエリを入力します。これを見てください:https://stackoverflow.com/questions/44556790/query-with-mongoose-multiple-times-without-nesting – tumulr

答えて

0

Promise.all()内のすべてのクエリをラップして、目的の結果を得ることができます。 Promise.all()に渡されたクエリは同時に実行されます。以下のコードをチェックしてください。

Promise.all([ 
    User.find(), 
    Category.find(), 
    Content.find() 
]) 
.then(results=>{ 

    //results return an array 

    const [users,categories,content] = results; 

    console.log("users",users); 
    console.log("categories",categories); 
    console.log("contents",content); 

}) 
.catch(err=>{ 
    console.error("Something went wrong",err); 
}) 

あなたは、あなたは基本的にオブジェクトの代わりに配列を通過させるPromise.props()を使用することができるブルーバードライブラリを使用している場合。

Promise.props({ 
    users:User.find(), 
    categories : Category.find(), 
    content : Content.find() 
}).then(result=>{ 

    console.log("users",result.users); 
    console.log("categories",result.categories); 
    console.log("contents",result.content); 

}).catch(err=>{ 
    console.error("Something went wrong",err); 
}) 
+0

ありがとう、あまりにもバディ –

関連する問題