2017-04-06 17 views
10

Mam 2-kątną aplikację, w której wywołuję bazę Firebase za pośrednictwem żądania http. Jednakże, zawsze próbuję uruchomić funkcję, dostaję ten błąd:Funkcje chmurowe dla Firebase - Nie można załadować adresu URL: Nie ma nagłówka "Access-Control-Allow-Origin".

XMLHttpRequest cannot load https://us-central1-<my-projectId>.cloudfunctions.net/[email protected] 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…} 

Oto moja Funkcja Chmura dla Firebase index.js pliku:

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

I wtedy oto jak Dzwonię funkcja w składniku (zakładając, że żądanie HTTP jest wykonany pomyślnie):

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

wydaje się, że bez względu na to, co próbuję, wciąż uderzając ten sam problem. Próbowałem nawet wdrożyć to na moim hostowanym adresie URL bazy firewall i nadal daje mi ten błąd. Czy ktoś wie, co mogę tutaj zrobić?

Z góry dziękuję!

+0

To duplikat to pytanie [] (http://stackoverflow.com/questions/42755131/enabling-cors-in-cloud-functions -for-firebase) choć może nie wiedziałeś tego :) –

Odpowiedz

10

Więc popełniłem tu kilka kluczowych błędów.

  1. Przed zainicjowaniem aplikacji Firebase upewnij się, że wymaga ona cors.
  2. Upewnij się, że parametry spełniają funkcję cors().

Oto zaktualizowany kod 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)) 
     }) 
    }); 
+0

Pracowałem świetnie, dzięki! – user3777933

Powiązane problemy