简体   繁体   English

如何使用 ionic typescript 解决 firebase 未捕获错误?

[英]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.

相关问题 如何解决 firebase deploy Parsing error: Cannot read file - How to resolve firebase deploy Parsing error: Cannot read file 使用 graphql 查询时如何解决验证错误? (打字稿) - How to resolve validation error when querying with graphql? (typescript) “未捕获的错误:”在 Flutter Firebase 部署中 - "Uncaught error: " in Flutter Firebase deployment 未捕获的 typescript 错误:无法读取未定义的属性 - Uncaught typescript error: cannot read properties of undefined 未捕获的错误:服务数据库不可用(Firebase 和 Skypack) - Uncaught Error: Service database is not available (Firebase and Skypack) 如何解决此 firebase 导入错误? 找不到模块:错误:Package 路径。 不从 package 导出 - How do I resolve this firebase import error? Module not found: Error: Package path . is not exported from package Firebase 未捕获的类型错误:无法解析模块说明符“firebase/app”。 相对引用必须以“/”、“./”或“../”开头 - Firebase Uncaught TypeError: Failed to resolve module specifier "firebase/app". Relative references must start with either "/", "./", or "../" 未捕获的类型错误:无法解析模块说明符“firebase/app”。 相对引用必须以“/”、“./”或“../”开头 - Uncaught TypeError: Failed to resolve module specifier "firebase/app". Relative references must start with either "/", "./", or "../" 未捕获 Firebase:Firebase:错误(auth/invalid-api-key) - Uncaught Firebase: Firebase: Error (auth/invalid-api-key) 未找到模块:错误:无法解析“./Firebase” - Module not found: Error: Can't resolve './Firebase'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM