我尝试在角度应用程序中使用firebase函数。 我使用angularfire2库 结果:
{err:错误:响应无效的JSON对象。 在新的HttpsErrorImpl(http://localhost:4200/vendor.j ...}
const functions = require('firebase-functions');
const cors = require('cors')({
origin: true
});
exports.helloWorld = functions.https.onRequest((request, response) => {
cors(request, response, () => {
response.send('Hello from Firebase!');
});
});
import { Component, OnInit } from '@angular/core';
import { AngularFireFunctions } from 'angularfire2/functions';
import { Observable } from 'rxjs';
import { first } from 'rxjs/operators';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
message: Observable<string>;
message2: string;
constructor(private fns: AngularFireFunctions) {
}
ngOnInit() {
}
getfsf() {
this.fns.httpsCallable('helloWorld')({ text: 'Some Request Data' })
.pipe(first())
.subscribe(resp => {
console.log({ resp });
}, err => {
console.error({ err });
});
}
}
答案 0 :(得分:0)
这是我能够解决的方法。
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send({ "data": "Hello from Firebase!" });
})
我们需要这样做很奇怪,但是它有效!
答案 1 :(得分:0)
对于那些其他答案不起作用的用户,这是对我有用的,因为我使用@ angular / fire库和Firebase Hosting来托管我的角度应用程序:
我必须在我的firebase.json文件中添加一个重写功能:
"hosting": {
"rewrites": [
{
"source": "/project-name/us-central1/someFunction",
"function": "someFunction"
},
{
"source": "/project-name/us-central1/anotherFunction",
"function": "anotherFunction"
},
...
]
}
更多详细信息可以在这里找到:https://github.com/angular/angularfire/blob/master/docs/functions/functions.md#firebase-hosting-integration