// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const db = cloud.database()
const _ = db.command
const $ = db.command.aggregate
/**
* customers表和record表作连接
* 需要异步返回数据,否则返回的数据为null
*/
console.log('调用云函数查询设备信息(ID列表):' + event.deviceId)
return new Promise((resolve , reject) => {
db.collection('customers')
.aggregate()
.match({
_id:_.in(event.deviceId)
})
.lookup({
from:'record',
let:{
customersId:'$_id'
},
pipeline:$.pipeline()
.match(_.expr($.eq(['$uid','$$customersId'])))
.sort({
record_time:-1
})
.limit(12)
.done(),
as: 'recordList'
})
.end()
.then(res => {
console.log("返回结果列表:")
console.log(res.list)
// 通过resolve()返回结果
resolve(res)
})
.catch(err => reject(err))
})
}
//调用端
wx.cloud.callFunction({
name:'getDeviceInfo',
data:{
'deviceId':deviceId
},
success:function(res){
wx.showToast({
title: '更新成功',
icon:'success',
})
console.log(res)
},
faile:function(res){
}
})
小程序实例 需要异步返回数据,否则返回的数据为null
最新推荐文章于 2023-09-11 18:23:52 发布
本文介绍了一种使用云函数从两个集合中查询并连接数据的方法。具体来说,是从 customers 表和 record 表中根据 ID 列表查询设备相关信息,并通过聚合查询返回最新的记录。该方法利用了云数据库的高级查询特性。
858

被折叠的 条评论
为什么被折叠?



