Firebase 可调用函数 + CORS [英] Firebase Callable Function + CORS

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

问题描述

我正在尝试从我的应用程序调用可调用的云函数,但我面临 CORS 问题.

I'm trying to call a callable Cloud Function from my app, but I'm facing CORS issues.

我无法启用 Cors,因为我无权访问 onCall 函数上的请求和响应.这是我的功能:

I can't enable Cors since I don't have access to the request and response on the onCall function. This is my function:

exports.UserInvitation = functions.https.onCall((data, context) => {
  const email = data.email


  return new Promise((resolve, reject) => {
    admin.auth().createUser({
      email: email,
      emailVerified: false,
      password: password
    }).then(resolve).catch((err) => {
      console.error(err.code)
      reject(new functions.https.HttpsError(err.code, err.message))
    })
  })
})

我是这样称呼它的:

functions.httpsCallable('UserInvitation')({ email: this.input.value }).then((data) => {
      console.log('Sent invitation:', data)
})

Firebase 函数 package.json:

Firebase-functions package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "bluebird": "^3.5.1",
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "private": true
}

WEB SDK Firebase 版本:4.13.1

WEB SDK Firebase version: 4.13.1

推荐答案

对于来到这里搜索 firebase 可调用函数 cors 错误的其他任何人,这是我的清单:

For anybody else who has arrived here searching firebase callable functions cors errors, here's my checklist:

  1. 确保函数已部署.
  2. 确保函数名称正确.当它应该是 recalculateY 时,我正在调用 recalculateY.由于某种原因出现 cors 错误.
  3. 确保函数代码本身没有抛出错误.使用模拟器提供帮助. 这仍然没有引发 cors 错误了解一下很有帮助.
  4. 确保您的地区匹配 - 我正在使用 europe-west2.我必须在该区域中部署该功能,并使用该区域调用它.有一段时间,我认为如果函数名称正确,客户端会推断出正确的区域.事实并非如此.
  1. Ensure the function is deployed.
  2. Ensure the function name is correct. I was calling recalculatY when it should have been recalculateY. Got a cors error for some reason.
  3. Ensure the function code itself is not throwing an error. Use the emulator to help. This didn't throw a cors error still helpful to know.
  4. Ensure your regions match - I am using europe-west2. I had to both deploy the function with that region, and call it using the region. For a while, I assumed the client would infer the correct region if the function name was correct. That was not the case.

将可调用函数部署到特定区域:

Deploying a callable function to a specific region:

// This is the file in which you define your callable function.
const functions = require('firebase-functions');
...
exports.yourFunc = functions.region('europe-west2').https.onCall(async (data, context) => {
    ...
})

从客户端(在本例中为 vuejs Web 应用程序)调用特定区域中的函数:

Calling a function in a specific region from the client (in this case, a vuejs web app):

// In my case, this is a vuex store file, but it is safe to assume this is plain old javascript
import firebase from 'firebase/app'
import 'firebase/functions'
...
firebase.app().functions('europe-west2').httpsCallable('yourFunc')

注意:firebase.app().function... vs firebase.app.function...

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

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