uniCloud JQL 查询使用问题

294 天前
 xiaohantx
post user
_id _id
title nickName
content
author

我需要返回 post 表所有内容并且根据 post 表的 author 字段去查 user 表的_id 字段,并把 nickname 单独这个字段拼到 post 查询结果里返回回来

db.collection('post')
    .aggregate()
    .lookup({
        from: 'uni-id-users',
        localField: 'author',
        foreignField: '_id',
        as: 'authorNickName'
    }).match({
        authorNickName: {
            title: '用户名'
        }
    }).end()

我看着是关联查询但是好像没办法查询起来,我看文档还有提到 pipeline ,我需要用到这个东西吗。。

524 次点击
所在节点    问与答
1 条回复
xiaohantx
294 天前
看了下采用了这个

```
let postTemp = await db.collection('post').field("title,content,author").getTemp()
let userTemp = await db.collection('uni-id-users').field("_id,nickName").getTemp()
let postResult = await db.collection(postTemp, userTemp).get()
```


但是一直在报错,不知道啥原因。。。

```
{ "code": "INVOKE_FUNCTION_FAILED", "message": "Cannot assign to read only property '0' of string 'title,content,author'", "stack": "TypeError: Cannot assign to read only property '0' of string 'title,content,author'\n at qt.field (/Applications/HBuilderX.app/Contents/HBuilderX/plugins/unicloud/aliyun/@dcloudio/serverless/lib/aliyun/uni-cloud.js:1:41743)\n at Proxy.getPost (/Users/han/Coding/hotPot/xxxxx/uniCloud-aliyun/cloudfunctions/client/controller/post.js:31:42)\n at a (/Users/han/Coding/hotPot/xxxxx/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:9940)\n at i (/Users/han/Coding/hotPot/xxxxx/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:6247)\n at auth (/Users/han/Coding/hotPot/xxxxx/uniCloud-aliyun/cloudfunctions/client/middleware/checkToken.js:21:9)\n at processTicksAndRejections (node:internal/process/task_queues:96:5)\n at async R (/Users/han/Coding/hotPot/xxxxx/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:7655)"}
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1014255

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX