【问题标题】:Angular Firebase deploy hosting issueAngular Firebase 部署托管问题
【发布时间】:2020-01-31 12:24:01
【问题描述】:

我正在尝试在 Firebase 托管上部署我的 Angular 应用程序。但它在加载程序后什么都没有显示,并且在控制台中显示错误。

TypeError: Object(...)(...).firestore is not a function

它在本地服务器上运行良好。知道如何解决吗?

env.prod.ts

export const environment = {
  production: true,
  firebase: {
    apiKey: 'xyzxaxasdaadsad',
    authDomain: 'localhost',
    databaseURL: 'https://puasda-e38c9.firebaseio.com/',
    projectId: 'puasda-e38c9',
    storageBucket: 'gs://puasda-e38c9.appspot.com',
    messagingSenderId: '205102099203'
  },

};

我的 app.module.ts

import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AppRoutingModule } from './app-routing.module';
import { SharedModule } from "./shared/shared.module";
import { AgmCoreModule } from '@agm/core';
import { HttpClientModule, HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { StoreModule } from '@ngrx/store';

import { SnotifyModule, SnotifyService, ToastDefaults } from 'ng-snotify';


import { AppComponent } from './app.component';
import { ContentLayoutComponent } from "./layouts/content/content-layout.component";
import { FullLayoutComponent } from "./layouts/full/full-layout.component";

import { DragulaService } from 'ng2-dragula';
import { AuthService } from './shared/auth/auth.service';
import { AuthGuard } from './shared/auth/auth-guard.service';

import * as $ from 'jquery';
import { LoginComponent } from './auth/login/login.component';
import { RegisterComponent } from './auth/register/register.component';
import { ForgotComponent } from './auth/forgot/forgot.component';
import { environment } from '../environments/environment';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpInterceptorService } from './helpers/http-interceptor/http-interceptor.service';
import { AngularFireModule } from '@angular/fire'; 
import { AngularFireAuthModule } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';
import { AngularFireStorageModule, StorageBucket } from '@angular/fire/storage';
import { ProductService } from './dashboard/add-product/product.service';
import { StoreFrontService } from './dashboard/store/store-front.service';

export function createTranslateLoader(http: HttpClient) {
    return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

@NgModule({
    declarations: [
        AppComponent,
        FullLayoutComponent,
        ContentLayoutComponent,
        LoginComponent,
        RegisterComponent,
        ForgotComponent
    ],
    imports: [
        BrowserAnimationsModule,
        StoreModule.forRoot({}),
        AppRoutingModule,
        SharedModule,
        SnotifyModule,
        HttpClientModule,
        NgbModule,
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: (createTranslateLoader),
                deps: [HttpClient]
            }
        }),
        AgmCoreModule.forRoot({
            apiKey: 'AIzaSyBr5_picK8YJK7fFR2CPzTVMj6GG1TtRGo'
        }),
        AngularFireModule.initializeApp(environment.firebase, 'PUASDA'),
        AngularFireAuthModule,
        AngularFireStorageModule,
        FormsModule,
        ReactiveFormsModule
    ],
    providers: [
        { provide: 'SnotifyToastConfig', useValue: ToastDefaults },
        {
            provide: HTTP_INTERCEPTORS,
            useClass: HttpInterceptorService,
            multi: true
        },
        SnotifyService,
        AuthService,
        AuthGuard,
        DragulaService,
        AngularFirestore,
        { provide: StorageBucket, useValue: 'gs://puasda-e38c9.appspot.com' },
        ProductService,
        StoreFrontService,
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

现在也附加 app.module.ts。我也不明白为什么会出现这个错误。任何的想法 ?

仅当我将应用程序上传到 Firebase 托管时才会显示此错误。并在本地主机或生产环境中工作

【问题讨论】:

  • 请发布您如何配置连接
  • @Mouna questio 已更新
  • Authdomain 不应该是 localhost。您可以在 firebase 控制台中找到配置。而且也可能是版本问题。 Firebase、rxjs 或 Angular Firestore
  • 您共享的代码不会抛出您发布的错误,因为它没有显示对firestore() 的任何调用。请编辑您的问题以包含minimum, complete/standalone code that any of us can run to reproduce the problem。我强烈建议阅读该链接,因为它包含有关如何最大限度地提高某人可以有效帮助您的机会的重要指导。
  • @Stratubas 是的,我已经有了那个设置。最后我发现了发生了什么。 Firebase 与 Angular 及其选项 optimization 的集成存在一个错误。如果您使用--optimization=false 构建prod,它会正确构建而不会出错...我在@angular/cli repo 上发现了这个问题,但我再也找不到它了。也许它已经固定在ng update 上。谢谢你的回答!

标签: angular firebase google-cloud-firestore


【解决方案1】:

添加您在 enviroment.ts 文件中添加的 firebase 的身份验证,也在 enviroment.prod.ts 中。

在您的 environment.prod.ts

export const environment = {
  production: true,
  firebase : {
    apiKey: "API KEY", //add your API and same for rest keys
    authDomain: "DOMAIN NAME",
    databaseURL: "DATA BASE URL",
    projectId: "PROJECT ID",
    storageBucket: "STORAGE BUCKET",
    messagingSenderId: "MESSAGING ID",
    appId: "AP ID",
    measurementId: "MEASUREMENT ID"
  }
};

【讨论】:

  • 编辑我的问题并附加 env.prod.ts api 密钥已添加
  • 您是否导入了import 'firebase/firestore'; 库? @UmaiZ
  • 不,我导入了这些 AngularFireModule、AngularFireAuthModule、AngularFirestore、AngularFireStorageModule。
  • 我想你是说这个 import { AngularFirestore } from '@angular/fire/firestore'; ?
  • 尝试导入firebase,从firebase导入*
【解决方案2】:

您的providers 中有AngularFirestore,但我认为您应该在imports 中使用AngularFirestoreModule

【讨论】:

    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2020-01-14
    • 2020-02-27
    • 1970-01-01
    • 2017-01-22
    • 2020-10-26
    相关资源
    最近更新 更多