[英]how to resolve a firebase Uncaught Error with ionic typescript?
I have a problem with my code.我的代码有问题。 I'm using the Firebase functions in an ionic project (with typescript).
我在离子项目中使用 Firebase 函数(使用打字稿)。 when I run "firebase serve", my function works and i can get the data, but when I export my function in ionic, I have this error:
当我运行“firebase serve”时,我的 function 工作并且我可以获得数据,但是当我在 ionic 中导出我的 function 时,我有这个错误:
Uncaught Error: Response is missing data field.
未捕获的错误:响应缺少数据字段。 at new HttpsErrorImpl (index.cjs.js:58) at Service.
在服务中的新 HttpsErrorImpl (index.cjs.js:58)。 (index.cjs.js:553) at step (tslib.es6.js:100) at Object.next (tslib.es6.js:81) at fulfilled (tslib.es6.js:71) at ZoneDelegate.invoke (zone-evergreen.js:359) at Object.onInvoke (core.js:39699) at ZoneDelegate.invoke (zone-evergreen.js:358) at Zone.run (zone-evergreen.js:124) at zone-evergreen.js:855
(index.cjs.js:553) 在步骤 (tslib.es6.js:100) at Object.next (tslib.es6.js:81) at fulfilled (tslib.es6.js:71) at ZoneDelegate.invoke (zone -evergreen.js:359) 在 Object.onInvoke (core.js:39699) 在 ZoneDelegate.invoke (zone-evergreen.js:358) 在 Zone.run (zone-evergreen.js:124) 在 zone-evergreen.js :855
this is my firebase function:这是我的 firebase function:
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
const serviceAccount = require('../permissions.json')
const cors = require('cors')({ origin: true })
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://{'myUrl'}.firebaseio.com'
})
export const posts = functions.https.onRequest(async (request, response) => {
const docs = await admin.firestore().collection('posts').orderBy('date', 'desc').get()
cors(request, response, () => {})
response.json(
docs.docs.map((doc) => {
return {
postID: doc.id,
...doc.data()
}
})
)
})
and this my ionic code:这是我的离子代码:
import { Component, OnInit } from '@angular/core';
import { AngularFireFunctions } from '@angular/fire/functions'
export class SharedModule {}
@Component({
selector: 'app-feed',
templateUrl: './feed.page.html',
styleUrls: ['./feed.page.scss'],
})
export class FeedPage implements OnInit {
posts
sub
constructor(private aff:AngularFireFunctions) {
}
ngOnDestroy() {
this.sub.unsubscribe()
}
ngOnInit() {
const posts = this.aff.httpsCallable('posts')
this.sub = posts({}).subscribe(data =>{
this.posts = data
} )
}
}
if anyone don't know why it isn't working, please push this question to the top with the up button, maybe other person knows it's super important because i really need to fix this error quickly如果有人不知道为什么它不起作用,请使用向上按钮将这个问题推到顶部,也许其他人知道它非常重要,因为我真的需要快速修复这个错误
thank you in advance先感谢您
You are invoking a callable function :您正在调用可调用的 function :
const posts = this.aff.httpsCallable('posts')
But your function isn't of the callalbe type.但是您的 function 不是 callalbe 类型。 It's an HTTP function , as you can see from
onRequest
in the function builder:这是一个HTTP function ,正如您从 function 构建器中的
onRequest
中看到的那样:
export const posts = functions.https.onRequest(async (request, response) => {
If you want to write a callable function, use the examples in the linked documentation.如果您想编写可调用的 function,请使用链接文档中的示例。 You will use
onCall
instead of onRequest
.您将使用
onCall
而不是onRequest
。 Note that they have very different APIs.请注意,它们具有非常不同的 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.