7***@qq.com
7***@qq.com
  • 发布:2025-06-27 23:39
  • 更新:2025-07-11 21:55
  • 阅读:107

使用云函数进行联表查询时,主表数据正常,副表结果为空(题主已自行处理)

分类:uniCloud

不知道为什么,副表中设置的"foreignKey",不能是主表自动生成的"_id",需要自己给主表数据一个id,才可以正常获取到数据。希望能帮到后续的人。若题主后续了解到原因了,会再来更新

主表dates字段为"_id",副表time_slots中,设置了dates_id的foreignKey为主表dates的_id

"dates_id": {  
      "description": "日期ID",  
      "foreignKey": "dates._id"  
   },

云函数,参考官方推荐写法联表查询

'use strict';  
exports.main = async (event, context) => {  
...  
const dates = db.collection('dates').where({date_time:db.command.gte(todayDate)}).orderBy("date_time").getTemp()  
const time_slots = await db.collection(dates,'time_slots').get()  
return time_slots;  
};  
...

返回的数据中,有完整的主表数据,但是每个主表数据下面的副表的time_slots都是空的。
这是什么原因

2025-06-27 23:39 负责人:无 分享
已邀请:
4***@tmp.dcloud.io

4***@tmp.dcloud.io

请问什么方法可以解决这个问题?

  • 7***@qq.com (作者)

    我自己在主表手工建了个唯一ID,然后副表绑定这个手工建的唯一ID,就可以了

    2025-07-20 08:32

要回复问题请先登录注册