为什么我通过react native expo查询Firebase实时数据库失败

30byixjq  于 2023-03-31  发布在  React
关注(0)|答案(1)|浏览(150)

有谁能解释一下为什么我对数据库的查询从数据库中什么也得不到?
验证码:

useEffect (() => {
  var random = 0
  get(ref(db, "food/Total")).then(snapshot => {
    const count = snapshot.val();
    console.log(count)
    random = Math.floor((Math.random() * count));
    console.log(random)
    const rc = query(ref(db, `food/`), orderByChild("ShopNo"), equalTo(random))
    get(rc)
      .then((querySnapshot) => {
        querySnapshot.forEach((shopSnapshot) => {
          const shopKey = shopSnapshot.key;
          console.log("Randomly selected shop: " + shopKey)
          const shopData = shopSnapshot.val();
          console.log("Shop data", shopData);
        });
      })
      .catch(error => {
        console.log(error);
      });
  });
}, []);

数据库(部分)

{
  "Bakery": {
    "Bakery Cuisine": {
      "Description": "Within North Spine Plaza",
      "Halal": "Yes",
      "Latitude": 1.34714,
      "Location": "50 Nanyang Ave, #01-20 North Spine Plaza, Singapore 639798",
      "Longitude": 103.68066,
      "OH": "Mon - Sat : 8 AM to 7 PM, Sun Closed",
      "ShopNo": 1
    }
  },
  "Beverage": {
    "Beverage": {
      "Description": "Within the South Spine food court",
      "Halal": "No",
      "Latitude": 1.34253,
      "Location": "21 Nanyang Link, Singapore 637371",
      "Longitude": 103.68243,
      "OH": "Mon - Fri: 7 30 am to 8 pm, Sat - Sun/PH Closed",
      "ShopNo": 2
    },
    "Beverages": {
      "Description": "Within North Spine Koufu",
      "Halal": "No",
      "Latitude": 1.34708,
      "Location": "76 Nanyang Dr, #02-03 North Spine Plaza, Singapore 637331",
      "Longitude": 103.68002,
      "OH": "Mon - Fri : 7 am to 8 pm, Sat : 7 am to 3 pm, Sun Closed",
      "ShopNo": 3
    },
    "Boost": {
      "Description": "Within North Spine Plaza",
      "Halal": "No",
      "Latitude": 1.34735,
      "Location": "50 Nanyang Ave, #01-11 North Spine Plaza, Singapore 639798",
      "Longitude": 103.68036,
      "OH": "Mon - Fri : 10 am to 9 pm, Sat - Sun: 10 am to 6 pm",
      "ShopNo": 4
    },
"Total": 89,
}

按理说,这个语句const rc = query(ref(db,food/), orderByChild("ShopNo"), equalTo(random))应该得到包含ShopNo的值的节点,该值与random的值匹配,但我什么也没有得到,没有任何错误。是语句的语法不正确还是我遗漏了其他代码?

hl0ma9xz

hl0ma9xz1#

ShopNo在深度路径中,也许您可以将数据库结构扁平化或拆分为多个查询。

query(ref(db, `food/Bakery `), orderByChild("ShopNo"), equalTo(random))
query(ref(db, `food/Beverage `), orderByChild("ShopNo"), equalTo(random))

相关问题