gpt4 book ai didi

javascript - 从另一个云函数调用一个云函数

转载 作者:IT老高 更新时间:2023-10-28 22:04:45 25 4
gpt4 key购买 nike

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

有没有一种特殊的方式来调用另一个云函数?还是只使用标准的 http 请求?

我试过像这样直接调用另一个函数:

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: 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 阻止了连接,尽管它是谷歌拥有的,因此它不应该被锁定

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

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

最佳答案

您无需通过全新的 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!");
}

这两个函数将做完全相同的事情,但端点不同。

关于javascript - 从另一个云函数调用一个云函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42784000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com