当我要根据不起作用的id过滤数据时,我已经设法从firebase.problem中获取数据了。
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AngularFireDatabase, AngularFireList ,} from 'angularfire2/database';
import { Observable } from 'rxjs'
import { query } from '@angular/core/src/render3';
import { Key } from 'protractor';
class postview {
constructor(public title) { }
}
@Component({
selector: 'app-news-view',
templateUrl: './news-view.page.html',
styleUrls: ['./news-view.page.scss'],
})
export class NewsViewPage implements OnInit {
id: string;
public books: AngularFireList<postview[]>;
itemlol: Observable<any>;
posts: Observable<any[]>;
constructor(private route: ActivatedRoute , db: AngularFireDatabase) {
let idp :string = this.id;
this.posts = db.list('Posts' ,ref => {
return ref.orderByChild('Post_Id').equalTo(idp)
}).valueChanges();
// this.itemlol= db.object('Posts').valueChanges();
}
ngOnInit() {
this.id = this.route.snapshot.paramMap.get('id');
console.log(this.id);
console.log(this.posts);
}
}在return ref.orderByChild('Post_Id').equalTo(idp)部分,我需要在equalTo()中传递变量。它应该根据用户说明进行更改
示例
equalTo(01)
equalTo(02)这是我的firebase数据库:

发布于 2019-03-10 15:47:07
构造函数在ngOnInit之前被调用,因此this.id的值在查询时是未定义的。
您应该在构造函数中获取参数
this.id = this.route.snapshot.paramMap.get('id');
let idp :string = this.id;
this.posts = db.list('Posts' ,ref => ref.orderByChild('Post_Id').equalTo(idp) ).valueChanges();https://stackoverflow.com/questions/55084918
复制相似问题