【问题标题】:Firebase real time database query sub childFirebase 实时数据库查询子子
【发布时间】:2018-09-14 18:30:29
【问题描述】:

这就是我的数据库的样子:

我设法通过查询userId 来获取所有文档:

this.orderListRef = firebase.database().ref(`/orderList`);
this.orderListRef.orderByChild('userId').equalTo(`${this.currentUser.uid}`)

但我在通过查询子键获取所有文档时遇到问题 restIdorder 数组中,我确实尝试了以下代码,但它只返回null

this.orderListRef.orderByChild('order/restId').equalTo('-L6vQHDBv9WugS_yTibn').on('value', snap => {
      console.log(snap.val())
    })

this.orderListRef.child('order').orderByChild('restId').equalTo('-L6vQHDBv9WugS_yTibn').on('value', snap => {
  console.log(snap.val())
})

请帮助.. 我想获得包含 "-L6vQHDBv9WugS_yTibn" 的返回,仅在 restId 键中,例如:

-L9IZKHkueFppGMJY3Z9: {deliveryAddress: "Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia", dtcreate: 1522894198149, order: Array(2), userId: "AeMgnwHy3Hav3FE9DyTMJVdp4QX2", userOrderNumber: "pGMJY3Z9"}
-L9I_DcXgx_fjsmsn2rm: {deliveryAddress: "Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia", dtcreate: 1522894433661, order: Array(2), userId: "AeMgnwHy3Hav3FE9DyTMJVdp4QX2", userOrderNumber: "jsmsn2rm"}
-L9IksHXG4WZo0ejfmk1: {deliveryAddress: "Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia", dtcreate: 1522897487221, order: Array(2), userId: "AeMgnwHy3Hav3FE9DyTMJVdp4QX2", userOrderNumber: "o0ejfmk1"}

提前谢谢你!

编辑 1: 在 Frank van Puffelen 建议创建 restaurant 作为 orderList > ID 的子项的方法后,我发现这是个好主意,我尝试了,但仍然没有运气。

这是我的数据库的新屏幕截图:

我尝试了以下查询,但每个人仍然返回null

this.orderListRef.orderByChild('restaurant').equalTo('rest001').once('value', snap => {
      console.log(snap.val())
    })

this.orderListRef.child('restaurant/rest001').once('value', snap => {
      console.log(snap.val())
    })
this.orderListRef.child('restaurant').equalTo('rest001').once('value', snap => {
      console.log(snap.val())
    })

this.orderListRef.child('restaurant').orderByChild('rest001').once('value', snap => {
      console.log(snap.val())
    })

感谢您的回复!

【问题讨论】:

    标签: angular typescript firebase firebase-realtime-database


    【解决方案1】:

    没有字段order/restId字段order/0/restIdorder/1/restId,但由于这不是固定路径,因此您无法查询它们。

    您当前的数据结构适合查找给定订单的餐厅 ID。它不太适合查找给定餐厅 ID 的订单。为此,您应该扩展您的数据结构以包含反转数据:

    restaurants
      restaurantId1
        order1: true
        order3: true
      restaurantId2
        order2: true
    

    现在您只需在/restaurants/$restaurantId 下查找指定餐厅的订单即可。

    有关更多信息,请参阅:

    【讨论】:

    • 不幸的是我仍然没有得到结果..我确实尝试了以下查询:this.orderListRef.orderByChild('restaurant').equalTo('rest001').once('value', snap => { console.log(snap.val()) }),`this.orderListRef.child('restaurant/rest001').once('value', snap => { console.log(snap.val()) })` , ` this.orderListRef.child('restaurant').equalTo('rest001').once('value', snap => { console.log(snap. val()) })` , this.orderListRef.child('restaurant').orderByChild('rest001').once('value', snap => { console.log(snap.val()) })
    • fyi,我添加了 restaurant 作为 orderList 的子项。 orderList > Id > restaurant 。然后我将rest001rest002 添加为restaurant 的子级,并将{order1:true} 添加为rest001rest002 的子级
    • 请使用新信息更新您的问题。您可能会注意到,cmets 中较长的代码几乎无法阅读。
    • 我已经更新了问题,请检查一下。感谢您的回复!
    • 这些查询都不会真正起作用,因为您经常在您调用它的上下文中不存在的路径上调用child()orderByChild()。我建议添加一个倒置数据结构,这在我对我链接的两个问题的回答中都有说明。
    猜你喜欢
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    相关资源
    最近更新 更多