简体   繁体   English

Angularfire2 Firestore没有从firebase中提取数据

[英]Angularfire2 Firestore not pulling data from firebase

I'm at a bit of a loss here. 我在这里有点不知所措。 I'm trying to learn angular cli + firebase2 (using angularfire2 5.0.0-rc.6) and I'm having an issue reading values from my cloud firestore database. 我正在尝试学习angular cli + firebase2(使用angularfire2 5.0.0-rc.6),我在从云端firestore数据库读取值时遇到问题。

I have followed several tutorials, as well as dug through the docs and by all measures from what I can tell it should be working. 我已经学习了几个教程,并通过文档挖掘了所有可以告诉它应该工作的措施。

My enviroments>enviroment.ts file is as follows (exact api strings redacted but copied directly from console web values). 我的环境> enviroment.ts文件如下(精确的api字符串已编辑,但直接从控制台Web值复制)。

export const environment = {
production: false,
  firebase: {
    apiKey: 'myapikey',
    authDomain: 'mydomain',
    databaseURL: 'mydburl',
    projectId: 'myprojectid',
    storageBucket: 'mystoragebucket',
    messagingSenderId: 'mymessageid'
  }
};

My app.module.ts is as follows: 我的app.module.ts如下:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MenubarComponent } from './menubar/menubar.component';
import { HomeComponent } from './home/home.component';
import {AppRoutingModule} from './app-routing/app-routing.module';
import { AngularFireModule } from 'angularfire2';
import { environment } from '../environments/environment';
import { HttpClientModule } from '@angular/common/http';
import { AngularFirestoreModule } from 'angularfire2/firestore';
import { SlideShowComponent } from './slide-show/slide-show.component';


@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule,
    HttpClientModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
  declarations: [AppComponent, HomeComponent, MenubarComponent, SlideShowComponent]
})
export class AppModule { }

My component (slide-show) slide-show.component.ts is as follows: 我的组件(幻灯片放映)slide-show.component.ts如下:

import { Component, OnInit } from '@angular/core';
import {AngularFirestore, AngularFirestoreCollection} from 'angularfire2/firestore';
import {Observable} from 'rxjs/Observable';


export interface Book {
  author: string;
  bgImage: string;
  cover: string;
  featured: boolean;
  published: boolean;
  teaser: boolean;
  title: boolean;
}

@Component({
  selector: 'app-slide-show',
  templateUrl: './slide-show.component.html',
  styleUrls: ['./slide-show.component.css']
})
export class SlideShowComponent implements OnInit {
  public collection: AngularFirestoreCollection<any>;
  public booksObservable: Observable<any[]>;
  public books: any[];

  constructor( afs: AngularFirestore ) {
    this.collection = afs.collection('books');
    this.booksObservable = this.collection.valueChanges();
    console.log( 'I am visible' );
    this.booksObservable.subscribe(data => {
      console.log(data);
      this.books = data;
    });
  }

  ngOnInit() {

  }

}

My slide-show.component.html is as follows: 我的slide-show.component.html如下:

<div class="container">
  <div class="spacer"></div>
  <div class="teaser">hello world test</div>
  <div class="menu-spacer">
    <ul>
      <li *ngFor="let item of booksObservable | async">
        {{item.author}}
      </li>
    </ul>
  </div>
  <div class="spacer"></div>
</div>

Due to the issues communicating I have changed my firestore.rules files to be the following: 由于问题沟通,我已将firestore.rules文件更改为以下内容:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

My database is set up as follows: 我的数据库设置如下: firebase数据库快照

My first thought was that I was reading from the observable wrong... so as several examples directly iterated over the observable in HTML I added that too... My second thought was that it might be because I was testing on my local server so I installed the firebase command line tools and tried running a bundled version of the angular site using firebase serve (built using both "ng build" and "ng build -prod"). 我的第一个想法是我正在从可观察到的错误中读取...因为几个例子直接在HTML中的observable上迭代我也添加了...我的第二个想法是它可能是因为我在我的本地服务器上测试所以我安装了firebase命令行工具,并尝试使用firebase服务(使用“ng build”和“ng build -prod”构建)运行角度站点的捆绑版本。 When that didn't work I attempted to host the built angular site on firebase. 当这不起作用时,我试图在firebase上托管构建的角度站点。 All resulted in the same response, I get nothing in my console (past the "I am visible" that I log to prove I am looking in the correct spot), then after some time (+5 min), occasionally, I get an empty array with the following console message: 所有这些都得到了同样的反应,我在我的控制台中什么也得不到(过去“我是可见的”我记录以证明我正在寻找正确的位置),然后经过一段时间(+5分钟),偶尔,我得到一个带有以下控制台消息的空数组:

Firestore (4.10.1) 2018-03-07T05:51:24.432Z: Could not reach Firestore backend.

When I failed to retrieve data from the collection I attempted to push data to a collection (at this point any would do) so I erased my database and attempted to use the following (inserted above line 29 in slide-show.component.ts) 当我无法从集合中检索数据时,我试图将数据推送到集合(此时任何操作都会这样做)所以我删除了我的数据库并尝试使用以下内容(在slide-show.component.ts中插入第29行)

this.collection.add({author: 'test1', title: 'test 1 title'});
this.collection.add({author: 'test2', title: 'test 2 title'});

This experiment resulted in both the front end console.log printing out the values, and the HTML printing the values (thanks RXJS) however, the firebase console never showed the additions. 这个实验导致前端console.log打印出值,HTML打印值(感谢RXJS)但是,firebase控制台从未显示添加内容。

I'm at a loss, feel free to ask for more information if you need it. 我很茫然,如果你需要,我可以随时询问更多信息。

I'm feeling like an idiot right now, but upon digging into this more I enabled the Logging of XML requests in chrome and started watching the actual RXJS queries, and noticed a continual failure of the request. 我现在感觉自己像个白痴,但是在深入研究这个问题后,我启用了chrome中的XML请求记录并开始观察实际的RXJS查询,并注意到请求的连续失败。 Thinking that was odd, I checked my add blockers (this one happened to be tunnlebear's blocker) and discovered it was blocking localhost. 考虑到这很奇怪,我检查了我的添加阻止程序(这个碰巧是tunnlebear的阻止程序)并发现它阻止了localhost。 After adding it as an exception I no longer had issues communicating with the firebase firestore. 添加它作为例外后,我不再遇到与firebase firestore通信的问题。

The issue was an ad blocker. 问题是广告拦截器。

Had this error in my console, working ong angular project: @firebase/firestore: Firestore (5.0.2): Could not reach Firestore backend. 在我的控制台中出现此错误,正在处理角度项目:@ firebase / firestore:Firestore(5.0.2):无法访问Firestore后端。

So I moved from Realtime Database to Cloud Firestore, and the error is gone. 所以我从实时数据库迁移到Cloud Firestore,错误消失了。 Maybe this error was stupid, and I left this comment just in case someone needs a solution. 也许这个错误是愚蠢的,我留下这个评论以防万一有人需要一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM