【问题标题】:Firebase API and Vue.JS get multiple elements by specific attributes valueFirebase API 和 Vue.JS 通过特定属性值获取多个元素
【发布时间】:2018-04-19 18:21:59
【问题描述】:

如何获取 Firebase (child) 中具有相同类别 ID 的所有元素?

getbyCateg: function(myCateg) {
      var items = [];
      FireBase.database().ref('product')
          .orderByChild('category')
          .equalTo(myCateg)
          .once("value", function(snapshot) {
              var key;
              snapshot.forEach(function (childSnapshot) {
                  key = childSnapshot.key;
                  return true;
              });
              if (key) {
                  items.push(FireBase.database().ref('product').child(key).toString());
              } else {
                  console.log("There is nothing of this category");
              }
          });
      return (items);

.once 允许访问具有良好类别 ID 的第一个元素,但其他元素呢?我想创建一个包含所有这些元素的数组。

谢谢!

【问题讨论】:

    标签: javascript firebase firebase-realtime-database vue.js promise


    【解决方案1】:

    在不了解您的数据结构的情况下,我不确定您的问题到底是什么。但也许这就是你需要的一切:

    getbyCateg: function(myCateg) {
      var items = [];
      FireBase.database().ref('product')
        .orderByChild('category')
        .equalTo(myCateg)
        .once("value", function(snapshot) {
          snapshot.forEach(function (childSnapshot) {
            key = childSnapshot.key;
            if (key) {
              // also, try this items.push(key.toString()) instead of next line
              items.push(FireBase.database().ref('product').child(key).toString());
            } else {
              console.log("There is nothing of this category");
            }
          });
        });
      return (items);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多