尝试了用promise,但输出编号发现还是异步执行
for(let i =0;i<res.data.length;i++)
{
var id = res.data[i].uid;
new Promise((resolve, reject) => {
console.log(id)
db.collection('Student')
.where({
_id:id
}).get()
.then(res=>{
if(res.data.length==0)
{
db.collection('Teacher')
.where({
_id:id
}).get()
.then(res=>{
that.data.headlist.push(res.data[0].pic)
that.setData({
headlist:that.data.headlist,
})
setTimeout(function () {
console.log(i)
resolve()
}, 500)
}).catch(res=>{
console.log(res)
})
}
else
{
that.data.headlist.push(res.data[0].pic)
that.setData({
headlist:that.data.headlist,
})
setTimeout(function () {
console.log(i)
resolve()
}, 500)
}
})
})
}
async await
let ids = res.data.map(v=>v.uid)//获得一个student id 的数组 db.collection('student').where({ _id:_.in(ids) //查询_id包含在ids的所以记录 }).get().then()
接口都自己写了,从服务端一把返回呗