比如
aModel = {
user_id: String,
file_name: String,
page_id: Number
}
fileSchema = new Schema({
page_id: Number,
key: String,
doc: String,
title: String,
sym: String
});
bModel = {
user_id: String,
file_name: String,
origin_file: String,
new_file: [fileSchema]
}
已知user_id,file_name,page_id,model是異步的,怎麼同時找這兩個model,
得到兩個結果後再進行下一步
我想大声告诉你2017-06-20 10:07:55
程式碼如下,其實我覺得寫的一點也不優雅...
var aDocs ,bDocs =null ;
aModel.find({ user_id:xx , file_name:xx , page_id:xx})
.then(function(docs){
aDocs = docs;
return bModel.find({ user_id:xx , file_name:xx , page_id:xx}) })
.then(function(docs){
bDocs = docs ;
other codes
})
女神的闺蜜爱上我2017-06-20 10:07:55
如果你需要他們並發執行的話,可以用Promise
const query1 = aModel.find(query)
const query2 = bModel.find(query)
Promise.all([query1, query2]).then(rs => {})
如果不需要並發進行,可以同步進行的話,可以用 yield
或 async/await
const query1 = yield aModel.find(query)
const query2 = yield bModel.find(query)
// 需要node7以上
const query1 = await aModel.find(query)
const query2 = await bModel.find(query)