收藏
回答

云开发联表查询出现权限不足问题, 已经设置集合为所有人可读了, 为什么还是会报错?

报错信息

WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1 Error: errCode: -502003 database permission denied | errMsg: Permission denied
    at new b (<anonymous>:1:163765)
    at success (<anonymous>:1:174759)
    at Function.success (WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1)
    at p (WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1)
    at Object.success (WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1)
    at a (WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1)
    at Mu (WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1)
    at Nu (WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1)
    at WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1
    at s (WAServiceMainContext.js?t=wechat&s=1696681160487&v=3.0.1:1)(env: Windows,mp,1.06.2307260; lib: 3.0.1)


联表查询语句如下

db.collection('Post').aggregate()
.unwind("$comments") // 展开 comments 数组
.lookup({
from: "User", // 关联的集合名
localField: "_openid", // 本地字段
foreignField: "_openid", // 关联字段
as: "user_docs" // 存放联表查询结果的数组字段名
})
.addFields( {
"user": {
"name": { $arrayElemAt: [ "$user_docs.name", 0 ] },
"avatar": { $arrayElemAt: [ "$user_docs.avatar", 0 ] },
"gender": { $arrayElemAt: [ "$user_docs.gender", 0 ] },
"department": { $arrayElemAt: [ "$user_docs.department", 0 ] },
"grade": { $arrayElemAt: [ "$user_docs.grade", 0 ] },
}
}
)
.lookup({
from: "User", // 关联的集合名
localField: "comments.openid", // 本地字段
foreignField: "_openid", // 关联字段
as: "comment_user_docs" // 存放联表查询结果的数组字段名
})
.addFields({
"comments.user": {
"name": { $arrayElemAt: [ "$comment_user_docs.name", 0 ] },
"avatar": { $arrayElemAt: [ "$comment_user_docs.avatar", 0 ] },
"gender": { $arrayElemAt: [ "$comment_user_docs.gender", 0 ] },
"department": { $arrayElemAt: [ "$comment_user_docs.department", 0 ] },
"grade": { $arrayElemAt: [ "$comment_user_docs.grade", 0 ] }
}
})


.project({
"user_docs": 0,
"comment_user_docs":0
})
// 第四个阶段:重新组合 comments 为数组
.group({
_id: "$_id",
_openid: { $first: "$_openid" },
text: { $first: "$text" },
timestamp: { $first: "$timestamp" },
date: { $first: "$date" },
imgs: { $first: "$imgs" },
user: { $first: "$user" },
comments: {
$push: "$comments"
}
})
.sort({timestamp:-1})
.skip(0)
.limit(2)
.end()



单独查post就没问题

db.collection('Post').where({
        _id: _.in(res.data[0][this.page])
      }).get().then(res=>{
        this.setData({
          articles: res.data
        })
      })


Post和User集合的权限设置都是下图这样



回答关注问题邀请回答
收藏

1 个回答

  • 陈宇明
    陈宇明
    2023-10-09

    你是在小程序端掉用的吗?需要在云函数里面掉用

    2023-10-09
    有用
    回复
登录 后发表内容