简体   繁体   English

Cloud Functions for Firebase - 无法加载 URL:不存在“Access-Control-Allow-Origin”标头

[英]Cloud Functions for Firebase - Cannot load URL: No 'Access-Control-Allow-Origin' header is present

I have an angular 2 app where I'm calling a Firebase via an http request.我有一个 angular 2 应用程序,我通过 http 请求调用 Firebase。 However, anytime, I try to run the function, I'm getting this error:但是,无论何时,当我尝试运行该函数时,都会收到此错误:

XMLHttpRequest cannot load https://us-central1-<my-projectId>.cloudfunctions.net/getUserByEmail?email=user@email.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.
    error_handler.js:54 EXCEPTION: Response with status: 0  for URL: null
    ErrorHandler.handleError @ error_handler.js:54
    next @ application_ref.js:348
    schedulerFn @ async.js:93
    SafeSubscriber.__tryOrUnsub @ Subscriber.js:234
    SafeSubscriber.next @ Subscriber.js:183
    Subscriber._next @ Subscriber.js:125
    Subscriber.next @ Subscriber.js:89
    Subject.next @ Subject.js:55
    EventEmitter.emit @ async.js:79
    NgZone.triggerError @ ng_zone.js:333
    onHandleError @ ng_zone.js:294
    ZoneDelegate.handleError @ zone.js:338
    Zone.runTask @ zone.js:169
    ZoneTask.invoke @ zone.js:420
    Subscriber.js:238 Uncaught Response {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers…}

Here is my Cloud Function for Firebase index.js file:这是我的 Cloud Function for Firebase index.js文件:

   const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
    const cors = require('cors')({origin: true});

    /**
     *  Function to get a user by email
     */
    exports.getUserByEmail = functions.https.onRequest((req, res) => {
      cors(request, response, () => {

// This should return a promise from the getUserByEmail function
response.status(200).send(admin.auth().getUserByEmail(req.query.email))
       })
    });

And then here's how I'm calling the function in a component (assuming the HTTP request is made successfully):然后这是我在组件中调用函数的方式(假设 HTTP 请求成功):

this.http.get('https://us-central1-rosebud-9b0ed.cloudfunctions.net/getUserByEmail', {
       search: "user@email.com"
     }).subscribe(data => {
       console.log('data', data);
     })

It would seem that no matter what I try, I keep hitting this same issue.似乎无论我尝试什么,我都会遇到同样的问题。 I've even tried deploying this to my firebase hosted URL and it still gives me this error.我什至尝试将它部署到我的 firebase 托管 URL,但它仍然给我这个错误。 Anyone know what I can do here?有谁知道我可以在这里做什么?

Thanks in advance!提前致谢!

So I made a few key mistakes here.所以我在这里犯了几个关键错误。

  1. Make sure to require cors before you initialize the Firebase app.在初始化 Firebase 应用之前,请确保需要cors
  2. Make sure that the parameters march the cors() function.确保参数行进cors()函数。

Here is the updated index.js code:这是更新后的index.js代码:

  const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    const cors = require('cors')({origin: true});
    admin.initializeApp(functions.config().firebase);


    /**
     *  Function to get a user by email
     */
    exports.getUserByEmail = functions.https.onRequest((request, response) => {
      cors(request, response, () => {

// This should return a promise from the getUserByEmail function
response.status(200).send(admin.auth().getUserByEmail(req.query.email))
       })
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase云功能显示错误“请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头。” - Firebase cloud function shows error “No 'Access-Control-Allow-Origin' header is present on the requested resource.” “CORS:不存在‘Access-Control-Allow-Origin’header”,但存在 - “CORS: No 'Access-Control-Allow-Origin' header is present ”, but it is present NodeJS / Firebase-Functions / Angular - CORS 策略已阻止从源访问站点:不存在“Access-Control-Allow-Origin” Z099FB995346F31C749F6E40DB0F395EZ - NodeJS / Firebase-Functions / Angular - Access to site from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present 角度:请求的资源上不存在“Access-Control-Allow-Origin”标头 - ANGULAR: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS:不存在“ Access-Control-Allow-Origin”标头Angular 2 - CORS : No 'Access-Control-Allow-Origin' header is present Angular 2 角度4-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Angular 4 - No 'Access-Control-Allow-Origin' header is present on the requested resource Angular 6 - 请求的资源上不存在“Access-Control-Allow-Origin”标头 - Angular 6 - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求中没有“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requeste 部署时某些路由中不存在“Access-Control-Allow-Origin”header - No “Access-Control-Allow-Origin” header is present in certain routes at deployment 请求的资源中不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resouce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM