问题描述
浏览文档时,我遇到了
〜源
有(引号中的链接)是有关functions.https.onCall
的提及.
但是在教程此处中,使用了另一个功能functions.https.onRequest
,所以我应该使用哪一个?为什么?它们之间的区别/相似之处是什么?
But in the tutorial here, another function functions.https.onRequest
is used, so which one should I use and why? What is the difference/similarity between them?
functions.https
的文档位于此处.
推荐答案
The official documentation for those is really helpful, but from the view of an amateur, the described differences were confusing at first.
- 这两种类型,在部署时均分配有唯一的HTTPS终结点URL,并且可以直接访问.
- Both types, when deployed, are assigned with a unique HTTPS endpoint URL and can be accessed directly.
-
可以直接从客户端应用程序调用(这也是主要目的).
Can be invoked (and this is also the main purpose) directly from the client app.
functions.httpsCallable('getUser')({uid})
.then(r => console.log(r.data.email))
它由用户提供的data
和 automagic context
实现.
It is implemented with user-provided data
and automagic context
.
export const getUser = functions.https.onCall((data, context) => {
if (!context.auth) return {status: 'error', code: 401, message: 'Not signed in'}
return new Promise((resolve, reject) => {
// find a user by data.uid and return the result
resolve(user)
})
})
context
自动包含有关请求的元数据例如uid
和token
.data
和response
对象会自动(反)序列化.context
automagically contains metadata about the request such as uid
and token
.data
and response
objects are automatically (de)serialized.- Firebase onRequest文档
- 主要用作Express API端点.
-
它由明确的
Request
和Response
对象实现.
- Firebase onRequest Docs
- Serves mostly as an Express API endpoint.
It is implemented with express
Request
andResponse
objects.
export const getUser = functions.https.onRequest((req, res) => {
// verify user from req.headers.authorization etc.
res.status(401).send('Authentication required.')
// if authorized
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify(user))
})
在此处了解更多信息新的Firebase Cloud Functions https.onCall触发器是否更好?
这篇关于Firebase云功能:onRequest和onCall之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!