【问题标题】:Can't get objectname out of objectobservable无法从 objectobservable 中获取 objectname
【发布时间】:2017-03-24 10:14:41
【问题描述】:

我遇到了一个问题,我想尝试从数据库中获取一些数据。我收到数据但我无法显示它,因为它是对象而不是字符串。如何将这些对象转换为字符串我已经尝试过一些安静的事情,但似乎没有任何效果。

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>

我收到此错误

内联模板:8:12 原因:找不到“object”类型的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。

【问题讨论】:

  • 对象是否在对象数组中?所以你需要用 *NgFor 迭代你的 html 组件
  • 当我删除 html 中的 .value 时,我确实发布了错误,但收到了同样的错误
  • 试试这个,告诉我你得到了什么
  • {{ item | json}}
  • 同样的错误已经尝试过了
  • 检查我关于将对象值推送到数组的答案
  • 标签: angular firebase firebase-realtime-database angularfire observable


    【解决方案1】:

    如果我没记错的话,您想显示艺术家发布的键列表: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>
    

    【讨论】:

      【解决方案2】:

      从你的 firebase 结构 artitemsid 你得到一个对象 {},你不能在一个对象上使用 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>
      

      【讨论】:

      • 0:Object 1:Object 2:Object 3:Object 4:Object 5:Object 6:Object 这是我现在在 console.log(myArray) 中的输出 xD
      • 我的 webapp 中确实有 10 个子弹。所以它看到里面有 10 个仍然没有价值
      • "Image" 她是我如何看待控制台日志的图像
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签