Friday, 17 March 2023

Why is my Firebase realtime database query failing via react native expo

Can anyone explain to me how come my query to the database get nothing from the database?

Code:

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);
      });
  });
}, []);

Database (Part of it)

{
  "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,
}

By right, this statement const rc = query(ref(db, `food/`), orderByChild("ShopNo"), equalTo(random)) should be getting the node that contains the ShopNo's value that matches the value of random but I am getting nothing at all with no errors. Is the syntax for the statement incorrect or am I missing additional codes?



from Why is my Firebase realtime database query failing via react native expo

No comments:

Post a Comment