这是我的组件。我不知道为什么无法从Firebase检索数据。谁能告诉我该代码中的错误吗?
import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html'
})
export class DashboardComponent implements OnInit {
tasks: FirebaseListObservable<any[]>;
constructor(
public afdb: AngularFireDatabase
) {
this.tasks = this.afdb.list('scheduler/')
console.log("Console", this.tasks);
}
ngOnInit() {
}
}
执行此代码时,我将获得此控制台-
{query: Reference, update: ƒ, set: ƒ, push: ƒ, remove: ƒ, …}
auditTrail
:
ƒ (events){query: Reference, update: ƒ, set: ƒ, push: ƒ, remove: ƒ, …}
auditTrail
:
ƒ (events)
答案 0 :(得分:0)
此代码的问题在于,当您可能想做的是记录可观察对象正在观看的数据时,您试图将可观察对象输出到控制台。
所以您可能想要更改:
console.log("Console", this.tasks);
对此:
this.tasks.subscribe(items => {
console.log(items);
});
答案 1 :(得分:0)
我得到答案的最终代码是-
import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html'
})
export class DashboardComponent implements OnInit {
tasks: FirebaseListObservable<any[]>;
constructor(
public afdb: AngularFireDatabase
) {
this.afdb.list('scheduler/').valueChanges().subscribe(items => {
console.log("items", items);
})
}
ngOnInit() {
}
}
答案 2 :(得分:0)
您可以通过这种方式从Firebase检索数据
import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
@Component({
selector: 'app-coupons',
templateUrl: './coupons.component.html',
styleUrls: ['./coupons.component.css']
})
export class CouponsComponent implements OnInit {
constructor(
private afdb: AngularFireDatabase
) { }
ngOnInit() {
}
getData() {
this.afdb.list('scheduler/').snapshotChanges()
.subscribe(values=>{
if(values) {
values.map(val=>{
let data = val.payload.val(); //get data
let uid = val.key;
});
}
});
}
}