[英]get data from angular firebase firestore database
I have Angular 6, "@angular/fire": "^5.1.1", "firebase": "^5.6.0"
, and I'm trying of fetch data of my database but it doesn't work. 我有Angular 6,
"@angular/fire": "^5.1.1", "firebase": "^5.6.0"
,我正在尝试获取数据库的数据,但是它不起作用。
i have a function called getCadenaFecha()
, here i have a logic that take any number of days and subtract them from the current date, then it store in cadena
to make a query and fetch data from firestore, when i return the variable ultimoDia
, it doesn't fetch the data 我有一个名为
getCadenaFecha()
的函数,在这里我有一个逻辑,该逻辑可以花费任意天数并从当前日期中减去它们,然后将其存储在cadena
以进行查询并从firestore中获取数据,当我返回变量ultimoDia
,它不会获取数据
This is my code: 这是我的代码:
import { Injectable } from '@angular/core';
import { tempFirebaseRepo } from './../Repository/tempFirebase'
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import 'rxjs/add/operator/map';
import {Dispositivo} from './../models/Dispositivo';
import {Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class query {
dispositivoRef:AngularFirestoreCollection<Dispositivo>;
dispositivoRefUno:AngularFirestoreCollection<Dispositivo>;
dispositivoRefDos:AngularFirestoreCollection<Dispositivo>;
dispositivoRefTres:AngularFirestoreCollection<Dispositivo>;
ultimaSemana: Observable<Dispositivo[]>;
ultimoDia: Observable<Dispositivo[]>;
constructor(private afs:AngularFirestore, private _tempFirebaseRepo:tempFirebaseRepo){}
getDia(){
let cadena=this._tempFirebaseRepo.getCadenaFecha(20)
//aca esta la query de Firestore
this.dispositivoRef = this.afs.collection('Dispositivo', ref =>{
return ref.where('Dispositivo', '>', Number(cadena))
})
debugger;
this.ultimoDia = this.dispositivoRef.valueChanges();
return this.ultimoDia;
}
}
....and this is my app.module: ....这是我的app.module:
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import {ChartsModule} from 'ng2-charts';
import {routing, appRoutingProviders} from './app.routing';
import {environment} from '../environments/environment';
import {AngularFirestoreModule} from '@angular/fire/firestore';
import {AngularFireModule} from '@angular/fire';
import { tempFirebaseRepo } from './Repository/tempFirebase';
//componentes
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component';
import { SettingsComponent } from './components/settings/settings.component';
import { MenuComponent } from './components/menu/menu.component';
import { DataComponent } from './components/menu/data/data.component';
import { InformeMesComponent } from './components/menu/informe/informe-mes/informe-mes.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
SettingsComponent,
MenuComponent,
DataComponent,
InformeMesComponent,
],
imports: [
HttpClientModule,
BrowserModule,
ChartsModule,
routing,
AngularFirestoreModule,
AngularFireModule.initializeApp(environment.firebase),
],
providers: [
appRoutingProviders,
tempFirebaseRepo
],
bootstrap: [AppComponent]
})
export class AppModule { }
...this is my component, here is the subscribe: ...这是我的组件,这是订阅:
import { Component, OnInit, Input} from '@angular/core';
import {TemperaturaService} from '../../../services/Temperatura/Temperatura.service';
//import {tempFirebaseService} from '../../../services/tempFirebase/temp-firebase.service';
import {Global} from '../../../services/Temperatura/global';
import {tempFirebaseRepo} from '../../../Repository/tempFirebase'
import {Observable, Subject} from 'rxjs/Rx';
import {query} from '../../../Repository/query'
@Component({
selector: 'app-data',
templateUrl: './data.component.html',
styleUrls: ['./data.component.css'],
providers: [TemperaturaService, /*tempFirebaseService*/ ]
})
export class DataComponent implements OnInit {
public data:any[];
public url: string;
public Chart = [];
public dia=[];
public mes=[];
public year=[];
public hora=[];
public minuto=[];
public medicion=[];
public fecha=[];
public Fecha=[];
public lineChartData:Array<any>=[{data: [], label: ''}];
public lineChartLabels:Array<any>=this.Fecha;
public currentDate:number;
constructor(
//private _tempFirebaseService: tempFirebaseService,
private _tempFirebaseRepo:tempFirebaseRepo,
private _query: query
) {
this.url = Global.url;
}
ngOnInit() {
this.getDia();
}
//Obtener con api rest
getDia(){
this._query.getDia().subscribe(
response => {
this.lineChartData=this._tempFirebaseRepo
.OrdenarResponse(response,
this.fecha,
this.medicion,
this.dia,
this.mes,
this.year,
this.hora,
this.minuto,
this.Fecha,
this.lineChartData
);
debugger;
},
error =>{
console.log(<any>error);
}
);
}
/*getSemana(){
this._tempFirebaseService.getUltimaSemana().subscribe(
response => {
this.lineChartData=this._tempFirebaseRepo
.OrdenarResponse(response,
this.fecha,
this.medicion,
this.dia,
this.mes,
this.year,
this.hora,
this.minuto,
this.Fecha,
this.lineChartData
);
debugger;
},
error =>{
console.log(<any>error);
}
);
}*/
/* getMes(){
this._tempFirebaseService.getUltimoMes().subscribe(
response => {
this.lineChartData=this._tempFirebaseRepo
.OrdenarResponse(response,
this.fecha,
this.medicion,
this.dia,
this.mes,
this.year,
this.hora,
this.minuto,
this.Fecha,
this.lineChartData
);
debugger;
},
error =>{
console.log(<any>error);
}
);
}*/
// lineChart
public lineChartOptions:any = {
responsive: true
};
public lineChartColors:Array<any> = [
{ // grey
backgroundColor: 'rgba(148,159,177,0.2)',
borderColor: 'rgba(148,159,177,1)',
pointBackgroundColor: 'rgba(148,159,177,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(148,159,177,0.8)'
},
{ // dark grey
backgroundColor: 'rgba(77,83,96,0.2)',
borderColor: 'rgba(77,83,96,1)',
pointBackgroundColor: 'rgba(77,83,96,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(77,83,96,1)'
},
{ // grey
backgroundColor: 'rgba(148,159,177,0.2)',
borderColor: 'rgba(148,159,177,1)',
pointBackgroundColor: 'rgba(148,159,177,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(148,159,177,0.8)'
}
];
public lineChartLegend:boolean = true;
public lineChartType:string = 'line';
// events
public chartClicked(e:any):void {
console.log(e);
}
public chartHovered(e:any):void {
console.log(e);
}
}
....the response always is empty ....响应始终为空
....this is my database: ....这是我的数据库:
You are setting up ultimoDia
as an observable, and then never subscribing to it. 您正在将
ultimoDia
设置为可观察的,然后再不订阅它。 Read up on observables and subscriptions , and you'll understand why your code isn't working. 阅读有关Observables和subscriptions的内容 ,您将了解为什么您的代码无法正常工作。 Basically, you are saying " This is the data I will want to listen to " but then you're never actually beginning to listen.
基本上,您说的是“ 这是我要收听的数据 ”,但实际上您从未真正开始收听。
Your mistake is a common one - the AngularFire2 docs are horrible for people unfamiliar with observables/subscriptions. 您的错误是一个常见错误-AngularFire2文档对于不熟悉可观察项/订阅的人来说非常可怕。 Include a
.subscribe()
to see your data, something like: 包含一个
.subscribe()
来查看您的数据,例如:
return this.dispositivoRef.valueChanges().subscribe(queriedItems => {
console.log(queriedItems);
return queriedItems;
});
* Untested code * 未经测试的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.