假設有已經存在的學生字典數組,學生有姓名和所在教室號,先查詢符合條件的學校,便利學生字典數組,創建學生對象賦值姓名、學校和班級,但是班級需要先查詢教室的位置來確定,這樣就出現循環查詢了?這樣的問題該怎麼解決呢?
var studentArr = new Array({'name': 'a','room':'101'},{'name': 'b','room':'102'},{'name': 'c','room':'103'},{'name': 'd','room':'104'});
var objects = new Array();
var schoolQuery = new AV.Query(Shcool);
schoolQuery.equalTo('name','**高中');
schoolQuery.find().then(function(schoolReuslts){
for (var i = 0; i < studentArr.count; i ++){
var student = studentArr[i];
var object = new Student();
object.set('name',student['name']);
object.set('room',student['room']);
object.set('school',schoolReuslts[0]);
var classQuery = new AV.Query(Class);
classQuery.equalTo('school',schoolReuslts[0]);
classQuery.equalTo('room',student['room']);
classQuery.find().then(function(classResults){
object.set('class',classResults[0]);
objects.push(object);
}, function(error){
console.log(error);
});
}
return AV.Object.saveAll(objects);
}).then(function(objects){
//全部保存成功
}
}).catch(function(error) {
console.log(error);
可以使用非同步函數的嵌套,async/await node.js版本>7.10.0