从另一个云函数调用一个云函数 [英] Calling a Cloud Function from another Cloud Function

查看:50
本文介绍了从另一个云函数调用一个云函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用云函数调用免费 spark 层上的另一个云函数.

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

有没有一种特殊的方式来调用另一个云函数?还是只使用标准的 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.

这篇关于从另一个云函数调用一个云函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆