Tôi có ứng dụng 2 góc cạnh nơi tôi đang gọi Firebase qua yêu cầu http. Tuy nhiên, bất cứ lúc nào, tôi cố gắng chạy chức năng, tôi nhận được lỗi này:Chức năng đám mây cho Firebase - Không thể tải URL: Không có tiêu đề 'Access-Control-Allow-Origin' hiện diện
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…}
Đây là chức năng đám mây của tôi cho căn cứ hỏa lực index.js
file:
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))
})
});
Và rồi Đây là cách tôi đang gọi chức năng trong một thành phần (giả sử yêu cầu HTTP được thực hiện thành công):
this.http.get('https://us-central1-rosebud-9b0ed.cloudfunctions.net/getUserByEmail', {
search: "[email protected]"
}).subscribe(data => {
console.log('data', data);
})
Có vẻ như dù tôi cố gắng thế nào đi nữa, tôi vẫn tiếp tục gặp vấn đề tương tự. Tôi thậm chí đã cố gắng triển khai này vào URL lưu trữ Firebase của tôi và nó vẫn mang lại cho tôi lỗi này. Có ai biết tôi có thể làm gì ở đây không?
Cảm ơn trước!
Đây là một bản sao của [câu hỏi này] (http://stackoverflow.com/questions/42755131/enabling-cors-in-cloud-functions -cho-firebase) mặc dù bạn có thể không biết điều đó :) –