从另一个云功能调用云功能

时间:2022-05-20 16:02:02

I am using a Cloud Function to call another Cloud Function on the free spark tier.

我正在使用Cloud Function在免费的Spark层上调用另一个Cloud Function。

Is there a special way to call another Cloud Function? Or do you just use a standard http request?

是否有一种特殊的方式来调用另一个云功能?或者您只是使用标准的http请求?

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))
})

But I get the error

但是我得到了错误

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

FetchError:请求https://us-central1-functions-****.cloudfunctions.net/validate失败,原因:getaddrinfo ENOTFOUND us-central1-functions - *****。cloudfunctions.net us-central1-functions - ***** cloudfunctions.net:443

Which sounds like firebase is blocking the connection, despite it being a google owned, and therefore it shouldn't be locked

哪个听起来像firebase阻止连接,尽管它是谷歌拥有的,因此它不应该被锁定

the Spark plan only allows outbound network requests to Google owned services.

Spark计划仅允许对Google拥有的服务的出站网络请求。

How can I make use a Cloud Function to call another Cloud Function?

如何使用云功能调用另一个云功能?

1 个解决方案

#1


7  

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:

您无需通过全新的HTTPS调用来解决调用某些共享功能的麻烦。您可以简单地将代码的常见位抽象为一个由任一个调用的常规javascript函数。例如,您可以像这样修改模板helloWorld函数:

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.

这两个函数将完全相同,但具有不同的端点。

#1


7  

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:

您无需通过全新的HTTPS调用来解决调用某些共享功能的麻烦。您可以简单地将代码的常见位抽象为一个由任一个调用的常规javascript函数。例如,您可以像这样修改模板helloWorld函数:

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.

这两个函数将完全相同,但具有不同的端点。