无法从 objectobservable 中获取 objectname
Can't get objectname out of objectobservable
我遇到了一个问题,我想从数据库中获取一些数据。我收到数据但无法显示它,因为它是对象而不是字符串。我怎样才能将这些对象放入字符串中?我尝试了一些安静的东西,但似乎没有任何效果。
Component.ts
this.artistitems = af.database.object('/users/'+this.artistid, {preserveSnapshot: true});
this.artistitems.subscribe(snapshot => {
this.artistvalues = snapshot.val();
this.artistitemsid = this.artistvalues.releases;
console.log(this.artistitemsid)
});
从 artistitemsid 在控制台中输出
aroundthefur:Object
b-sides&rarities:Object
diamondeyes(deluxe):Object
gore:Object
koinoyokan:Object
saturdaynightwrist(explicitversion):Object
thestudioalbumcollection:Object
数据库
我尝试放入 component.html 崩溃的所有内容我真的不知道如何从对象中获取值而不是获取对象本身
当我在我的 component.html
<ul>
<li *ngFor="let item of artistitemsid">
{{ item.value }}
</li>
</ul>
我收到这个错误
inline template:8:12 caused by: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
从你的 firebase 结构 artistitemsid 你得到一个对象 {},你不能在对象上使用 ngFor。
但是您应该能够通过插值直接访问这些值:
{{artistitemsid.gore.value}}
现在取决于值,如果它是一个对象数组,那么您可以使用 ngFor
对其进行迭代
或者您可以将获得的值添加到数组中:
你从 firebase 得到类似
的东西
{aroundthefur:{},b-sides&rarities:{},...}
.
this.artistitems.subscribe(snapshot => {
this.artistvalues = snapshot.val();
this.artistitemsid = this.artistvalues.releases;
const myArray = [];
for (let key in this.artistitemsid){
myArray.push(this.artistitemsid[key]);
}
this.items = myArray; // items is declared outside so you can use it on a ngFor
console.log(myArray)
});
在 ngOnInit 上使用里面的函数,你会得到类似的东西:
items = [aroundthefur:{},b-sides&rarities:{},...]
并且可以用作
<ul>
<li *ngFor="let item of items">
{{ item.value }} // basically is aroundthefur.value
</li>
</ul>
如果我没记错的话,您想显示艺术家发行的关键列表:aroundthefur、b-sides&rarities 等
因此您可以从 releases
对象中提取这些键:
this.artistitems = af.database.object('/users/'+this.artistid, {preserveSnapshot: true});
this.artistitems.subscribe(snapshot => {
this.artistvalues = snapshot.val();
this.artistitemsid = Object.keys(this.artistvalues.releases);
});
然后用 ngFor
遍历结果数组:
<ul>
<li *ngFor="let item of artistitemsid">
{{ item}}
</li>
</ul>
我遇到了一个问题,我想从数据库中获取一些数据。我收到数据但无法显示它,因为它是对象而不是字符串。我怎样才能将这些对象放入字符串中?我尝试了一些安静的东西,但似乎没有任何效果。
Component.ts
this.artistitems = af.database.object('/users/'+this.artistid, {preserveSnapshot: true});
this.artistitems.subscribe(snapshot => {
this.artistvalues = snapshot.val();
this.artistitemsid = this.artistvalues.releases;
console.log(this.artistitemsid)
});
从 artistitemsid 在控制台中输出
aroundthefur:Object
b-sides&rarities:Object
diamondeyes(deluxe):Object
gore:Object
koinoyokan:Object
saturdaynightwrist(explicitversion):Object
thestudioalbumcollection:Object
数据库
我尝试放入 component.html 崩溃的所有内容我真的不知道如何从对象中获取值而不是获取对象本身
当我在我的 component.html
<ul>
<li *ngFor="let item of artistitemsid">
{{ item.value }}
</li>
</ul>
我收到这个错误
inline template:8:12 caused by: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
从你的 firebase 结构 artistitemsid 你得到一个对象 {},你不能在对象上使用 ngFor。
但是您应该能够通过插值直接访问这些值:
{{artistitemsid.gore.value}}
现在取决于值,如果它是一个对象数组,那么您可以使用 ngFor
对其进行迭代或者您可以将获得的值添加到数组中:
你从 firebase 得到类似
的东西{aroundthefur:{},b-sides&rarities:{},...}
.
this.artistitems.subscribe(snapshot => {
this.artistvalues = snapshot.val();
this.artistitemsid = this.artistvalues.releases;
const myArray = [];
for (let key in this.artistitemsid){
myArray.push(this.artistitemsid[key]);
}
this.items = myArray; // items is declared outside so you can use it on a ngFor
console.log(myArray)
});
在 ngOnInit 上使用里面的函数,你会得到类似的东西:
items = [aroundthefur:{},b-sides&rarities:{},...]
并且可以用作
<ul>
<li *ngFor="let item of items">
{{ item.value }} // basically is aroundthefur.value
</li>
</ul>
如果我没记错的话,您想显示艺术家发行的关键列表:aroundthefur、b-sides&rarities 等
因此您可以从 releases
对象中提取这些键:
this.artistitems = af.database.object('/users/'+this.artistid, {preserveSnapshot: true});
this.artistitems.subscribe(snapshot => {
this.artistvalues = snapshot.val();
this.artistitemsid = Object.keys(this.artistvalues.releases);
});
然后用 ngFor
遍历结果数组:
<ul>
<li *ngFor="let item of artistitemsid">
{{ item}}
</li>
</ul>