如何自动更改用于调用Firebase函数的路径 [英] How can I change a path automatically for calling firebase functions

查看:80
本文介绍了如何自动更改用于调用Firebase函数的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于Firebase环境构建我的项目. 对于发送GET和DELETE调用,不仅要发送POST调用.我已使用.onRequest方法注册了我的函数(因为.onCall仅支持POST调用,因此我已在文档中进行了检查)

I'me making my project base on firebase environment. And for sending GET and DELETE call not only POST call. I'd registered my function with method .onRequest (because .onCall only supports POST call, I've checked this in document)

现在我想从前端调用函数.

And now I wanted to call my function from frontend.

所以我用这个来接电话.

So I fetched call with this.

fetch(API_ROOT+"/api/sentences", {
  method: "GET"
})

所以我不得不使用我的条件来不依赖开发环境或分布式环境.

And so I had to use my condition for not relying on the environment, dev or dist.

let API_ROOT = ""
if (location.hostname == "127.0.0.1" || location.hostname == "localhost") {
  console.log("This is local emulator environment")
  functions.useFunctionsEmulator("http://localhost:5001")
  API_ROOT = "http://localhost:5001/[my-project-id]/us-central1"
}else{
  API_ROOT = "https://us-central1-[my-project-id].cloudfunctions.net" 
}

但是我应该使用这个吗?还是有任何类似于.onCall注册的.httpsCallable方法.

But should I use this? Or is there any way similar to the .httpsCallable method with .onCall registering.

因为不需要一个完整的呼叫路径,如下所示.

Because that one doesn't need a full path for calling, like below.

functions
  .httpsCallable("api/sentences")()
  .then((res) => console.log(res))

推荐答案

可调用的客户端SDK functions 仅用于与您使用onCall声明的函数一起使用(建议您再次阅读该文档).几乎可以肯定,它不能与用onRequest声明的普通HTTP触发器一起使用.可调用函数具有其非常特定的协议,并且不允许您更改函数的名称或路径URI.

The client SDK for callable functions is only intended to work with functions that you declare using onCall (I suggest reviewing that documentation again). It's almost certainly not expected to work with normal HTTP triggers declared with onRequest. Callable functions have their own very specific protocol and do not allow you to change the name or path URI of the function.

如果您有HTTP触发器,请跳过Firebase客户端库,仅像使用其他HTTP API一样使用常规HTTP客户端.

If you have an HTTP trigger, skip the Firebase client library and just use a regular HTTP client like you would with any other HTTP API.

这篇关于如何自动更改用于调用Firebase函数的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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