来自Firbease的数组被解释为聚合物中的对象,具有dom-repeat

时间:2016-12-30 15:27:16

标签: javascript arrays firebase polymer dom-repeat

我在Polymer中使用dom-repeat来重复从Firebase加载的数组,我无法弄明白,为什么我得到“预期的项目数组,找到对象”错误,如图所示下面的截图。这是我正在使用的代码:

    <data-firebase data="{{cardsetList}}" requested-location="card-sets"  user="{{user}}"></data-firebase>

    <template is="dom-repeat" items="[[cardsetList]]">
        <paper-card image="../images/trip.png" alt="Donuts" class="amber" >
          <div class="card-content">Test {{index}}</div>
        </paper-card>
    </template>

浏览器视图&amp;控制台

enter image description here

data-firebase 是我创建的一个从FB加载数据的元素,正如您在控制台中看到的那样成功(我想要加载2个对象的数组

加载前清空数组 我检查了加载前我的对象中是否有一些数组数据,但是你也可以在控制台中看到它只是一个空数组。如果我停止从Firebase加载数据,也没有错误

Firebase中有意义的数组 我还检查了FB中的数据,它表示为JSON数组(我可以看到,当我导出数据时,数据被导出到数组括号[])

问题:数据已加载,但错误仍然存​​在 如何进行dom重复(因为你可以看到它被渲染),并且在控制台中还有一个数组,但我仍然收到这个错误?

非常感谢!

亲切的问候, 添

3 个答案:

答案 0 :(得分:1)

我的理解是firebase中不支持数组,并且数组存储为&#34; object&#34;以整数作为关键名称。

// we send this
['hello', 'world']
// Firebase stores this
{0: 'hello', 1: 'world'} 

见这里:

https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

这看起来就像你要回来的那样。

答案 1 :(得分:0)

我明白了。在开始时,fb-auth似乎没有提供足够快的用户。因此,当用户为空时,firebase会尝试从不存在的位置加载数据,从而创建对象。

我添加了以下计算的绑定:

<强>功能

     hasContent: function(checkLength) {
         return checkLength.length > 0 ? checkLength : [];
     }

以HTML格式

<template is="dom-repeat" items="[[hasContent(cardsetList)]]" as="cardItemLoop">
    ...
</template>

但是我必须考虑如何防止在没有用户提供的情况下进行查询/数据绑定。

此外,这似乎也是指firebase-document defaults to {} before resolving #33

答案 2 :(得分:0)

如果没有延迟,为了防止查询,请删除您的路径,稍后当您的auth对象在js中设置路径时。 因此,您的查询将没有路径,然后在您的标记中以及您的身份验证的成功回调中,您可以设置如下所示的路径。

  signInFb: function() {
    this.$.auth.signInWithRedirect().then(function(response) {
      this.$.yourQueryElementID.path = "your path here" ; 
    }.bind(this)).catch(function(error) {

    }.bind(this));
  },