laf/docs/guide/function/call-function.md
全场最拉 fd6fe91d9b
doc: update how to use cloudfunction related doc (#750)
* doc: update package-manage.md

* doc: add cloudfunction docs

* doc: add use-env doc

* doc: upload image file into project directory

---------

Co-authored-by: sulnong <bytepay@bytepayment.com>
2023-02-09 20:23:42 +08:00

58 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 在云函数中调用
---
# {{ $frontmatter.title }}
云函数在开发完毕并发布后,可以在其他云函数中进行调用。
## 编写并发布待调用云函数
比如,我们创建一个名为 `get-user-info` 的云函数,并编写如下代码:
```typescript
import cloud from '@lafjs/cloud'
const db = cloud.database()
export async function main(ctx: FunctionContext) {
const { body: { userid } } = ctx
if (!userid) return { err: 1, errmsg: 'userid not exists' }
const userCollection = db.collection('user')
const { data: user } = await userCollection.where({ id: userid }).get()
if (!user) return { err: 2, errmsg: 'user not found' }
return { err: 0, data: user }
}
```
该函数接收一个名为 userid 的参数, 并通过id在数据库中查找相应用户并将 查找到的数据返回。
## 调用已发布云函数
`get-user-info` 云函数发布后, 我们可以在其他云函数调用该函数。
```typescript
import cloud from '@lafjs/cloud'
export async function main(ctx: FunctionContext) {
const res = await cloud.invoke('get-user-info', { ...ctx, body: { userid: 'user id' }})
console.log(res)
}
```
我们通过调用 `cloud.invoke` 方法可以在云函数中调用其他云函数。
该方法接收两个参数:第一个参数为 待调用的函数名,第二个参数为传递的数据
可以看到,`body` 传入了我们请求参数如果函数内部需要使用ctx中的某些属性还可用 `...ctx` 的方式传入ctx以便被调用函数使用。