获取firebase数据库上limit(1)查询的子对象

时间:2017-11-08 10:24:48

标签: javascript firebase-realtime-database

我有这个数据

enter image description here

我做这个查询

snapshot.first().price

我得到了这个结果

enter image description here

然后问题是

  

是否有一种简单的方法可以访问 one 对象的price属性,该对象的密钥我不知道?
  例如snapshot.only().pricevar result = snapshot.val() var key = Object.keys(result)[0] var price = result[key].price

简单地说,我想避免这个

StockinId - all shelfs in location
+---------+----------+-------+
| country | location | bin   |
+---------+----------+-------+
| LV      | AL       | A-1-1 |
| LV      | AL       | A-1-2 |
| LV      | AL       | A-1-3 |
| LV      | AL       | A-1-4 |
| LV      | AL       | A-1-5 |
| LV      | AL       | A-1-6 |
| LV      | AL       | A-1-7 |
| LV      | AL       | A-1-8 |
| LV      | AL       | A-1-9 |
+---------+----------+-------+


Calculated - calculated amount which should be on the shelf 
+-------------------+----------+-------+
| calculated_needed | location | bin   |
+-------------------+----------+-------+
| 2                 | AL       | A-1-1 |
| 4                 | AL       | A-1-2 |
| 15                | AL       | A-1-3 |
| 5                 | AL       | A-1-4 |
| 22                | AL       | A-1-5 |
+-------------------+----------+-------+

Inventory - actual amount counted physically recounting
+--------+----------+-------+---------------+
| amount | location | bin   | item          |
+--------+----------+-------+---------------+
| 2      | AL       | A-1-1 | 2600000741897 |
| 4      | AL       | A-1-2 | 2600000741897 |
| 5      | AL       | A-1-4 | 2600000999045 |
| 22     | AL       | A-1-5 | 2600000998949 |
| 6      | AL       | A-1-6 | 2600000998925 |
| 99     | AL       | A-1-7 | 2600000998871 |
+--------+----------+-------+---------------+

1 个答案:

答案 0 :(得分:0)

对Firebase数据库执行查询时,可能会有多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。

在回调中,您需要使用snapshot.forEach()

来处理此列表
firebase.database().ref(${path}/buy)
  .orderByChild('price').limitToFirst(1)
  .once('value')
  .then(snapshot => { 
      snapshot.forEach(function(child) {
          console.log(child.val());
          console.log(child.val().price);
      }) 
  })