【问题标题】:Angular firebase hosting AT(...)firestore is not a functionAngular firebase 托管 AT(...)firestore 不是一个函数
【发布时间】:2019-11-05 16:47:35
【问题描述】:

我为测试 firebase 功能创建了 angular 应用程序,并将此应用程序部署在 firebase 托管上。几乎所有工作都需要 Firestore 功能。 这是错误

main.8ae925009adf8b80e1bc.js:1 ERROR Error: Uncaught (in promise): TypeError: AT(...).firestore is not a function
TypeError: AT(...).firestore is not a function

我不知道为什么会出现这个错误。在我当地的环境中,一切正常。这是应用程序https://elevated-analog-119716.firebaseapp.com的链接

我用这个库

"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/cdk": "~7.3.7",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/fire": "^5.2.1",
"@angular/forms": "~7.2.0",
"@angular/http": "~7.2.0",
"@angular/material": "^7.3.7",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"@ngxs/store": "^3.4.3",
"angularfire2": "^5.2.1",
"core-js": "^2.5.4",
"firebase": "^6.2.2",
"nativescript-angular": "~7.2.0",
"nativescript-theme-core": "~1.0.4",
"reflect-metadata": "~0.1.12",
"rxjs": "~6.3.3",
"tns-core-modules": "~5.2.0",
"zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular/cli": "^7.2.0",
    "@angular/compiler-cli": "~7.2.0",
    "@angular-devkit/build-angular": "~0.11.4",
    "@nativescript/schematics": "~0.4.0",
    "@types/jasmine": "2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "nativescript-dev-typescript": "~0.8.0",
    "nativescript-dev-webpack": "^0.20.0",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~3.1.1"
  }

这是我的应用模块

    @NgModule({
  declarations: [
    AppComponent,
    MenuComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    AngularFireModule.initializeApp(firebaseConfig),
    AngularFireDatabaseModule,
    BrowserAnimationsModule,
    LayoutModule,
    MatToolbarModule,
    MatButtonModule,
    MatSidenavModule,
    MatIconModule,
    MatListModule,
    HomeModule,
    PolicyListModule,
    MatTableModule,
    AddUserModule
  ],
  providers: [PolicyService, AngularFirestore],
  bootstrap: [AppComponent]
})

这是 usersService 我调用数据的地方

constructor(private firestore: AngularFirestore) {}
    getUsers(): Observable<any> {
        return this.firestore.collection('user', x => x.orderBy('jerk', 'asc')).snapshotChanges();
    }

这是来自组件的代码

  ngOnInit() {
    this.db.getUsers().subscribe(v => this.items = v.map(v =>{
      const data = v.payload.doc.data();
      data.id = v.payload.doc.id;
      return data;
    }));
  }

【问题讨论】:

    标签: angular typescript firebase google-cloud-firestore angularfire2


    【解决方案1】:

    不需要提供者AngularFirestore 它已经在模块AngularFirestoreModule 中声明,记住现在你正在导入错误的模块。 PolicyService 可能相同,但我不确定不知道这个模块。

    通过导入AngularFireDatabaseModule,您可以编写查询,如this.db.listthis.db.object,当您导入AngularFirestoreModule 时,您可以编写查询,如this.db.collection,或者如果您需要一份文档this.db.doc

    粘贴到服务文件中:

      addKeyToObject = _ => {
        const object = _.payload.val()
        object.key = _.payload.key;
        return object;
      }
    
      constructor(private firestore: AngularFirestore) { }
    
      getUsers(): Observable<any[]> {
        return this.firestore.collection('user', x => x.orderBy('jerk', 'asc')).snapshotChanges()
          .pipe(map(actions => actions.map(this.addKeyToObject)))
      }
    

    如果这是一个服务文件,你可能想在 providers: [UsersService] 的 app.module.ts 中声明它

    在component.ts中:

      users$: Observable<any[]>
    
      constructor(private db: AngularFirestore) { }
    
      ngOnInit() {
        this.users$ = this.db.getUsers()
      }
    

    在这个组件的模板中:

    <div *ngFor="let user of users$ | async">
      <p>User id: {{user.id}}</p>
    </div>
    

    | async 是一个管道,它将阻止您订阅 Observable。 如果你真的想订阅,你需要取消订阅。

    版主删除了我上一个答案,所以我发布了新答案。 ;] 糟糕的是我无法在帖子中发表评论。 ;/

    【讨论】:

    • 感谢您的回答。当我从提供程序中删除 AngularFirestore 时,出现错误 ERROR 错误:未捕获(承诺中):错误:StaticInjectorError(AppModule)[UserService -> AngularFirestore]: StaticInjectorError(Platform: core)[UserService -> AngularFirestore]: NullInjectorError: No provider for AngularFirestore !错误:StaticInjectorError(AppModule)[UserService -> AngularFirestore]: StaticInjectorError(Platform: core)[UserService -> AngularFirestore]: NullInjectorError: No provider for AngularFirestore!
    • 当我运行 ng build --optimization=false 时它开始工作
    • 您遇到错误,因为您导入的是实时数据库而不是云存储,抱歉没有看到。您需要导入AngularFirestoreModule
    • 通过导入 AngularFireDatabaseModule 来编写查询,如 this.db.listthis.db.object,当你导入 AngularFirestoreModule 时,你编写查询,如 this.db.collection 或者如果你需要一个文档 this.db.doc
    • 为了清楚明白这一点,你们想说,我会说,在 app.module.ts 中,添加 `import { AngularFirestoreModule } from '@angular/fire/firestore'; ` 并将这个 AngularFirestoreModule 放入 imports 数组中。
    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多