【问题标题】:@angular/fire: ERROR TypeError: Cannot read property 'firebaseApp' of undefined@angular/fire:错误类型错误:无法读取未定义的属性“firebaseApp”
【发布时间】:2019-03-13 15:00:23
【问题描述】:

我正在使用: "@angular/core": "^6.1.0", ... "@angular/fire": "^5.0.2", “火力基地”:“^5.3.1”,

如果在我的 app.modules.ts 中有:

import { AngularFireModule } from '@angular/fire';
...
imports: [
...
AngularFireModule.initializeApp(environment.firebase),
...
]

在环境/environment.ts 中使用我的 firebase 配置设置,

并且在我的 authService 中有

initAuthListener() {
        this.afAuth.authState.subscribe(user => { ... }
}

然后我得到以下控制台错误,并且:

ERROR TypeError: Cannot read property 'firebaseApp' of undefined
    at Object.get [as app] (http://localhost:4200/vendor.js:153576:31)
    at http://localhost:4200/vendor.js:162834:9
    at Array.forEach (<anonymous>)
    at deepFreeze (http://localhost:4200/vendor.js:162831:33)
    at http://localhost:4200/vendor.js:162837:7
    at Array.forEach (<anonymous>)
    at deepFreeze (http://localhost:4200/vendor.js:162831:33)
    at http://localhost:4200/vendor.js:162837:7
    at Array.forEach (<anonymous>)
    at deepFreeze (http://localhost:4200/vendor.js:162831:33)

如果我随后按照https://github.com/angular/angularfire2/blob/master/docs/install-and-setup.mdoptional 自定义 FireBaseApp 名称添加到 app.modules.ts,例如 AngularFireModule.initializeApp(environment.firebase, 'firebaseApp'), 或任何替代自定义应用程序名称,例如“123App”。控制台错误消失,firebase 工作正常。

(更新:不接触此代码,仅更新了另一个区域的一些 firebase 功能,现在即使使用自定义应用程序名称,我也收到上述错误!发生了什么)

为什么我需要设置可选名称才能使其工作,即使该名称可以是随机的并且没有实际意义,因为在我的代码中的其他任何地方都没有使用?

同样在 vscode 中,app.modules.ts import { AngularFireModule } from '@angular/fire'; 的 @angular/fire 部分错误带有下划线,并带有 msg [ts] cannot find module 'angularfire2',一切正常,但是我需要包含什么 @types 来清除它或有自从我最近从“angularfire2”更改为“@angular/fire”后,vscode 刚刚出现了一个缓存问题?

【问题讨论】:

  • 要解决第二个问题,[ts] cannot find module 'angularfire2' 错误,在 tsconfig.json 中包含 "baseUrl": "./src"
  • 您使用的是 Angular CLI 还是自定义 webpack?
  • 使用角度cli。
  • 可能是 npm install 的问题。删除 node_modules, npm cache clean --verify npm install
  • 感谢 Suresh,肯定需要更新我的 npm 模块,但这不是问题。这是我的代码。

标签: angular typescript angularfire5


【解决方案1】:

最后,这是由于可观察的 firebase Ref 作为 InitAuthLister 的一部分存储到我的 Store State 中,然后由于某种原因变得未定义,即使使用 observable.pipe(take(1).map(...) .subscriber(...)。我什至不需要 Ref 只需要 Id。现在都解决了。

因此,请注意存储 firebase refs 的位置/方式,因为它们在某些时候可能会超出范围。

【讨论】:

  • 是的,我将整个authState 退回到我的商店,这超出了我的需要!谢谢
猜你喜欢
  • 2021-12-16
  • 2021-11-05
  • 2019-04-15
  • 2018-11-21
  • 2020-05-25
  • 2018-08-29
  • 2019-06-28
  • 2019-11-04
  • 2020-01-20
相关资源
最近更新 更多