微信小程序查询云数据库表的多个数据
循环是两次了,但得到的数据都是基于最后一个数据来查询的,就是查询不准确,但又不想设置延时器,等这个查询完再执行下一个,就看到下面这个的提示,就意识到不能循环体来执行。就想着能不能一次把列表里的数据查阅完,借助chatgpt知道了有map这个方法,来一次查询,然后得到了正确的结果。
·
在微信小程序有可能需要通过一个表的数据来查询另一个表的数据,例如评论区里的用户头像和昵称是需要评论表里的用户id来去到用户表里查头像和昵称,假如执行循环体来查询:
commentList=res.data;
for(var i=0;i<commentList.length;i++){
// 根据评论表里的openid获取到用户头像和昵称
console.log("目前下标"+i)
db.collection("table_customer").where({
_openid:commentList[i]._openid
}).get({
success:res=>{
console.log(res.data)
console.log(commentList)
console.log(commentList[i-1])
commentList[i-1].photo=res.data[0].photo;
commentList[i-1].name=res.data[0].name;
console.log(commentList[i-1])
that.data.responderList.push(commentList[i-1]);
that.setData({
responderList:that.data.responderList
})
console.log(that.data.responderList)
}
})
}
循环是两次了,但得到的数据都是基于最后一个数据来查询的,就是查询不准确,但又不想设置延时器,等这个查询完再执行下一个,就看到下面这个的提示,就意识到不能循环体来执行循环中频繁查询数据库优化_循环查询数据库的性能问题及优化_璃墟的博客-CSDN博客
就想着能不能一次把列表里的数据查阅完,借助chatgpt知道了有map这个方法,来一次查询,然后得到了正确的结果
commentList=res.data;
commentList.map(comment => {
// 根据评论表里的openid获取到用户头像和昵称
db.collection("table_customer").where({
_openid:comment._openid
}).get({
success:res=>{
comment.photo=res.data[0].photo;
comment.name=res.data[0].name;
console.log(comment)
that.data.responderList.push(comment);
that.setData({
responderList:that.data.responderList
})
console.log(that.data.responderList)
}
})
})

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)