我有一个角度2的应用,我通过http请求呼叫Firebase。但是,无论何时,我尝试运行该功能,我都会收到此错误:
Flowable<Tuple<Long, Integer>> findLineMarkers(
Flowable<List<Tuple<Long, Integer>>> charPairs,
BiFunction<Tuple<Long, Integer>, Optional<Tuple<Long, Integer>>, Optional<Tuple<Long, Integer>>> strategy) {
return charPairs().map(pair -> {
Tuple<Long, Integer> fst = pair.get(0);
Optional<Tuple<Long, Integer>> snd = pair.size() > 1 ? Optional.of(pair.get(1)) : Optional.empty();
return strategy.apply(fst, snd);
}).filter(Optional::isPresent).map(Optional::get);
}
Flowable<SplitInfo> split(InputStream stream) throws IOException {
return findLineMarkers(stream, startingPositionStrategy)
.zipWith(findLineMarkers(stream, endingPositionStrategy),
(s, e) -> new Split(s.item1, e.item1 - s.item1))
.filter(split -> !removeEmptyLines || split.length > 0)
.zipWith(counter(), Tuple::new)
.timeInterval(TimeUnit.MILLISECONDS)
.map(x -> new SplitInfo(x.value().item1.start,
x.value().item1.length,
x.value().item2,
x.time(), x.unit()));
}
以下是我的Firebase 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…}
文件云功能:
index.js
接下来是我如何调用组件中的函数(假设HTTP请求成功):
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))
})
});
似乎无论我尝试什么,我都会遇到同样的问题。我甚至尝试将此部署到我的firebase托管网址,它仍然给我这个错误。谁知道我能在这做什么?
提前致谢!
答案 0 :(得分:16)
所以我在这里犯了几个关键错误。
cors
。cors()
功能。以下是更新的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))
})
});