如何使用 Ionic 4 从 Firebase 获取值
How to Get a Value from Firebase Using Ionic 4
抱歉提问。我只是一个菜鸟:(
使用 Ionic 4 + Firebase,只是为了学习,假设我在 html 上有一个带有(单击)功能的按钮,并且希望能够获得一个值!!!用头撞墙!
示例:
Firebase DB:
collection "Places"
doc:
id: 1
name: somename
想点击按钮并通过硬编码 id,我想提醒(somename)
当我点击我得到 [object 对象] 时,无法进入该对象。
代码:
export class Tab4Page implements OnInit {
lugar: AngularFirestoreCollection<Places>;
lugares: Observable<Places[]>;
constructor(private _angFireStore: AngularFirestore) {
this.lugar = this._angFireStore.collection('Places');
this.lugares = this.lugar.valueChanges();
}
ngOnInit() { }
//this is the function
pruebaF1() {
alert(this.lugares); //What's next here???!!
}
}
抱歉,我听不到。
不知道如何进一步深入 this.lugares 以硬编码一个 id 并只获取一个值:比如说,获取 nombre where id = 1,然后 nombre = somename
如果我说 id = 2,那么 nombre = 该文档在数据库中的名称。
不angular明智:(
提前致谢!!!! :)
我看得出来你真的很努力。我经常看到这样的场景,并且总是想知道人们最初是如何走到这一步的。我很好奇,您是在遵循指南还是只是尝试随机代码?
最好的做法是遵循一些入门教程,这些教程将引导您完成基础知识的每个步骤。先做他们想教你的事,然后通过编辑位开始构建你自己的,直到你发现自己写出整个东西没有问题。
This seems like an interesting guide, or the official angularfire2 docs.
基本上您遗漏了一些概念,这些概念最好在编写良好的教程中进行解释。到目前为止,您从代码中得到的是可观察的。可以在前端使用类似这样的东西:
<ul>
<li *ngFor="let item of lugares | async">
{{ item.name }} is {{ item.price }} (assuming these properties exist)
</li>
</ul>
但你实际上问的不是这个。您正在编写代码来访问一个集合,但要获得一个 where id = 1
类型的查询是一个文档,它更像是:
this.placeRef = this.afs.collection(''Places'', ref => ref.where('id', '==', '1'));
但是这仍然会让您对之后的处理方式缺乏理解。我认为最好的办法是深入研究入门文档。
顺便说一句,您拥有的原始标签,firebase 实时数据库是旧数据库和一种非常不同的技术,因此如果您要搜索信息,您需要确保使用正确的 "firestore" 搜索词。
抱歉提问。我只是一个菜鸟:( 使用 Ionic 4 + Firebase,只是为了学习,假设我在 html 上有一个带有(单击)功能的按钮,并且希望能够获得一个值!!!用头撞墙! 示例:
Firebase DB:
collection "Places"
doc:
id: 1
name: somename
想点击按钮并通过硬编码 id,我想提醒(somename)
当我点击我得到 [object 对象] 时,无法进入该对象。 代码:
export class Tab4Page implements OnInit {
lugar: AngularFirestoreCollection<Places>;
lugares: Observable<Places[]>;
constructor(private _angFireStore: AngularFirestore) {
this.lugar = this._angFireStore.collection('Places');
this.lugares = this.lugar.valueChanges();
}
ngOnInit() { }
//this is the function
pruebaF1() {
alert(this.lugares); //What's next here???!!
}
}
抱歉,我听不到。
不知道如何进一步深入 this.lugares 以硬编码一个 id 并只获取一个值:比如说,获取 nombre where id = 1,然后 nombre = somename 如果我说 id = 2,那么 nombre = 该文档在数据库中的名称。
不angular明智:(
提前致谢!!!! :)
我看得出来你真的很努力。我经常看到这样的场景,并且总是想知道人们最初是如何走到这一步的。我很好奇,您是在遵循指南还是只是尝试随机代码?
最好的做法是遵循一些入门教程,这些教程将引导您完成基础知识的每个步骤。先做他们想教你的事,然后通过编辑位开始构建你自己的,直到你发现自己写出整个东西没有问题。
This seems like an interesting guide, or the official angularfire2 docs.
基本上您遗漏了一些概念,这些概念最好在编写良好的教程中进行解释。到目前为止,您从代码中得到的是可观察的。可以在前端使用类似这样的东西:
<ul>
<li *ngFor="let item of lugares | async">
{{ item.name }} is {{ item.price }} (assuming these properties exist)
</li>
</ul>
但你实际上问的不是这个。您正在编写代码来访问一个集合,但要获得一个 where id = 1
类型的查询是一个文档,它更像是:
this.placeRef = this.afs.collection(''Places'', ref => ref.where('id', '==', '1'));
但是这仍然会让您对之后的处理方式缺乏理解。我认为最好的办法是深入研究入门文档。
顺便说一句,您拥有的原始标签,firebase 实时数据库是旧数据库和一种非常不同的技术,因此如果您要搜索信息,您需要确保使用正确的 "firestore" 搜索词。