假设有已经存在的学生字典数组,学生有姓名和所在教室号,先查询符合条件的学校,便利学生字典数组,创建学生对象赋值姓名、学校和班级,但是班级需要先查询教室的位置来确定,这样就出现循环查询了?这样的问题该怎么解决呢?
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