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

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

问题描述



有没有特别的方法可以调用另一个云端函数?或者你只是使用一个标准的http请求?



我已经尝试直接调用其他函数如下:

  exports.purchaseTicket = functions.https.onRequest((req,res)=> {
fetch('https:// us-central1 (= response.json())
.then(json => res.status(201))。 json(json))
})

但是我得到错误


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


这听起来像firebase阻止连接,尽管它是一个谷歌拥有,为此e它不应该被锁定


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


如何使用云端函数调用另一个云端函数?

解决方案

您不需要通过全新的HTTPS调用来调用某些共享功能。您可以简单地将常见的代码抽象成一个常规的JavaScript函数,它被任何一个调用。例如,你可以像下面这样修改helloWorld模板:

  var functions = require('firebase-functions'); 
$ b exports.helloWorld = functions.https.onRequest((request,response)=> {
common(response)
})

exports .helloWorld2 = functions.https.onRequest((request,response)=> {
common(response)
})

function common(response){
response.send(来自一个普通的老功能!);





$ b

这两个函数的功能完全相同,只是端点不同。 / p>

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

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

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

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

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?

解决方案

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天全站免登陆