firebase.init() 在新的 NativeScript 8.2.3 应用程序上不断崩溃

firebase.init() keeps crashing on new NativeScript 8.2.3 App

我最近创建了一个新的 NativeScript 8.2.3 项目来重新设计旧的现有项目,该项目是 运行 在 NativeScript 8.2.2 上。

我已经通过 npm i @nativescript/firebase 安装了 this NativeScript-Firebase-Plugin 并通过 tns plugin add @nativescript/firebase 将插件添加到项目中。在将 google-services.json 添加到我的 App_Resources 之后,我已经将 firebase.init() 函数实现到我的 app.component.ts 中,如下所示:

import { Component, OnInit } from '@angular/core'
import { firebase } from "@nativescript/firebase";


@Component({
  selector: 'ns-app',
  templateUrl: 'app.component.html',
})
export class AppComponent implements OnInit {
  ngOnInit() {

    
    firebase
    .init()
    .then(() => console.log("Firebase initialised!"))
    .catch( err => console.error(`Error: ${err}`));
  }
}

但在 运行 ns debug android 之后我得到以下 StackTrace:

System.err: An uncaught Exception occurred on "main" thread.
System.err: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException: Error calling module function 
System.err: TypeError: Cannot read property 'ads' of undefined
System.err: File: (file: src/webpack:/mtbappmobile/webpack/bootstrap:27:0)
System.err: 
System.err: StackTrace: 
System.err: ./node_modules/@nativescript/firebase/firebase.android.js(file: src/webpack:/mtbappmobile/node_modules/@nativescript/firebase/firebase.android.js:24:0)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/app/app.component.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:174:80)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/app/app.module.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:206:72)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/main.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:14:73)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at __webpack_exec__(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:274:39)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:275:189)
System.err:     at __webpack_require__.X(file: src/webpack:/mtbappmobile/webpack/runtime/startup entrypoint:6:0)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:275:47)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:280:3)
System.err:     at require(:1:266)
System.err: 
System.err: 
System.err: TypeError: Cannot read property 'ads' of undefined
System.err: 
System.err: StackTrace:
System.err: java.lang.RuntimeException: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException: Error calling module function 
System.err: TypeError: Cannot read property 'ads' of undefined
System.err: File: (file: src/webpack:/mtbappmobile/webpack/bootstrap:27:0)
System.err: 
System.err: StackTrace: 
System.err: ./node_modules/@nativescript/firebase/firebase.android.js(file: src/webpack:/mtbappmobile/node_modules/@nativescript/firebase/firebase.android.js:24:0)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/app/app.component.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:174:80)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/app/app.module.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:206:72)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/main.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:14:73)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at __webpack_exec__(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:274:39)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:275:189)
System.err:     at __webpack_require__.X(file: src/webpack:/mtbappmobile/webpack/runtime/startup entrypoint:6:0)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:275:47)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:280:3)
System.err:     at require(:1:266)
System.err: 
System.err: 
System.err: TypeError: Cannot read property 'ads' of undefined
System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5876)
System.err:     at android.app.ActivityThread.access00(ActivityThread.java:199)
System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
System.err:     at android.os.Looper.loop(Looper.java:193)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6669)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
System.err: Caused by: com.tns.NativeScriptException: Error calling module function 
System.err: TypeError: Cannot read property 'ads' of undefined
System.err: File: (file: src/webpack:/mtbappmobile/webpack/bootstrap:27:0)
System.err: 
System.err: StackTrace: 
System.err: ./node_modules/@nativescript/firebase/firebase.android.js(file: src/webpack:/mtbappmobile/node_modules/@nativescript/firebase/firebase.android.js:24:0)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/app/app.component.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:174:80)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/app/app.module.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:206:72)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at fn(file: src/webpack:/mtbappmobile/webpack/runtime/hot module replacement:62:0)
System.err:     at ./src/main.ts(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:14:73)
System.err:     at __webpack_require__(file: src/webpack:/mtbappmobile/webpack/bootstrap:24:0)
System.err:     at __webpack_exec__(file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:274:39)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:275:189)
System.err:     at __webpack_require__.X(file: src/webpack:/mtbappmobile/webpack/runtime/startup entrypoint:6:0)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:275:47)
System.err:     at (file:///data/data/com.maxtoolbox.vrsta/files/app/bundle.js:280:3)
System.err:     at require(:1:266)
System.err: 
System.err: 
System.err: TypeError: Cannot read property 'ads' of undefined
System.err:     at com.tns.Runtime.runModule(Native Method)
System.err:     at com.tns.Runtime.runModule(Runtime.java:689)
System.err:     at com.tns.Runtime.run(Runtime.java:681)
System.err:     at com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5871)
System.err:     ... 8 more

当我删除 firebase.init() 功能后,应用 运行 正常。您知道导致此问题的原因以及解决方法吗?

这是我的 package.json:

{
  "name": "mtbappmobile",
  "main": "src/main.ts",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "@angular/animations": "~13.1.1",
    "@angular/common": "~13.1.1",
    "@angular/compiler": "~13.1.1",
    "@angular/core": "~13.1.1",
    "@angular/forms": "~13.1.1",
    "@angular/platform-browser": "~13.1.1",
    "@angular/platform-browser-dynamic": "~13.1.1",
    "@angular/router": "~13.1.1",
    "@nativescript/angular": "^13.0.0",
    "@nativescript/core": "~8.2.1",
    "@nativescript/firebase": "^11.1.0",
    "@nativescript/theme": "~3.0.1",
    "nativescript": "^8.2.3",
    "nativescript-sentry": "^2.0.1",
    "rxjs": "~7.4.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~13.1.1",
    "@angular/compiler-cli": "~13.1.1",
    "@nativescript/android": "8.2.2",
    "@nativescript/ios": "~8.2.0",
    "@nativescript/types": "~8.2.0",
    "@nativescript/webpack": "~5.0.0",
    "@ngtools/webpack": "~13.1.1",
    "typescript": "~4.5.5"
  }
}

Nativescript TSC 维护了该插件的重写版本。看一看https://github.com/NativeScript/firebase

实施:

安装

npm install @nativescript/firebase-core
npm install @nativescript/firebase-auth
npm install @nativescript/firebase-storage
// And other Firebase packages you would like to use

导入和初始化

在您应用程序的主文件中(例如,app/app.js)尽快初始化 firebase 模块

import { firebase } from '@nativescript/firebase-core'
import '@nativescript/firebase-auth'; // only needs to be imported 1x
import '@nativescript/firebase-storage'; // only needs to be imported 1x
// And other Firebase packages


firebase().initializeApp().then(fbApp => {
  console.log("Firebase app initialized!", fbApp.name)

  // To use with the Firebase Emulator
  // firebase().auth().useEmulator('localhost', 9099);
  // firebase().storage().useEmulator('localhost', 9199);
  // ... and other emulator ports

  firebase().auth().addAuthStateChangeListener(async (user) => {
    if (!user) {
      // User is currently signed out!
    } else {
      // User is signed in!
    }
  })
  
  // Other modules' init functions
}