从另一个 Cloud Functions 调用 Cloud Functions [英] Calling a Cloud Function from another Cloud Function
问题描述
我正在使用 Cloud Functions 调用免费 Spark 层上的另一个 Cloud Functions 函数.
I am using a Cloud Function to call another Cloud Function on the free spark tier.
有没有一种特殊的方式来调用另一个 Cloud Functions 函数?或者你只是使用标准的 http 请求?
Is there a special way to call another Cloud Function? Or do you just use a standard http request?
我试过像这样直接调用另一个函数:
I have tried calling the other function directly like so:
exports.purchaseTicket = functions.https.onRequest((req, res) => {
fetch('https://us-central1-functions-****.cloudfunctions.net/validate')
.then(response => response.json())
.then(json => res.status(201).json(json))
})
但我收到错误
FetchError:请求到https://us-central1-functions-****.cloudfunctions.net/validate失败,原因:getaddrinfo ENOTFOUNDus-central1-functions-*****.cloudfunctions.netus-central1-functions-*****.cloudfunctions.net:443
FetchError: request to https://us-central1-functions-****.cloudfunctions.net/validate failed, reason: getaddrinfo ENOTFOUND us-central1-functions-*****.cloudfunctions.net us-central1-functions-*****.cloudfunctions.net:443
这听起来像是 firebase 阻止了连接,尽管它是谷歌拥有的,因此它不应该被锁定
Which sounds like firebase is blocking the connection, despite it being a google owned, and therefore it shouldn't be locked
Spark 计划只允许向 Google 拥有的出站网络请求服务.
the Spark plan only allows outbound network requests to Google owned services.
如何使用一个云函数调用另一个云函数?
How can I make use a Cloud Function to call another Cloud Function?
推荐答案
您无需经历通过全新 HTTPS 调用来调用某些共享功能的麻烦.您可以简单地将代码的常见部分抽象为一个常规的 javascript 函数,该函数由任一函数调用.例如,您可以像这样修改模板 helloWorld 函数:
You don't need to go through the trouble of invoking some shared functionality via a whole new HTTPS call. You can simply abstract away the common bits of code into a regular javascript function that gets called by either one. For example, you could modify the template helloWorld function like this:
var functions = require('firebase-functions');
exports.helloWorld = functions.https.onRequest((request, response) => {
common(response)
})
exports.helloWorld2 = functions.https.onRequest((request, response) => {
common(response)
})
function common(response) {
response.send("Hello from a regular old function!");
}
这两个函数将做完全相同的事情,但具有不同的端点.
These two functions will do exactly the same thing, but with different endpoints.
这篇关于从另一个 Cloud Functions 调用 Cloud Functions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!