Wednesday, 8 March 2023

Trouble with retrieving specific node in firebase realtime DB via react native expo

New issue that appear after Grabbing the wrong item from firebase realtime database via react native expo

Part of my database: enter image description here

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

My code

const SubScreen2 = () => {
  
  
  const navigation = useNavigation()
  
  const [todoData, setToDoData] = useState([])
  
  
  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("Hello")
          console.log("Randomly selected shop: " + shopKey)
          const shopData = shopSnapshot.val();
          console.log("Shop data", shopData);
        })
      .catch(error => {
        console.log(error);
      })
      })
      
      
    
      
  },) })

No errors reported in console but the only logs I get was

enter image description here

I tried catching the error but there was nothing appearing in my log. I am very confused about this. Why is there no error but at the same time, it cannot grab the node?

Using the same method but with function:

Code

const SubScreen2 = () => {
  
  
  const navigation = useNavigation()
  
  const [todoData, setToDoData] = useState([])
function getFirstChild(queryRef) {
  return get(query(queryRef, limitToFirst(1))) // mix in a query limit
    .then((querySnapshot) => {
      let firstChild = null;
      querySnapshot.forEach((childSnapshot) => {
        firstChild = childSnapshot;
      });
      return firstChild; // DataSnapshot | null
    });
}
  
  
  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))
      
  getFirstChild(rc) // rc being query(ref(db, "food"), orderByChild("ShopNo"), equalTo(random))
  .then((shopSnapshot) => {
    const shopKey = shopSnapshot.key;
    console.log("Randomly selected shop: " + shopKey)
    const shopData = shopSnapshot.val();
    console.log("Shop data", shopData);
  })
    })
  })

      
      
    
      
  },) })

I got a error of

enter image description here



from Trouble with retrieving specific node in firebase realtime DB via react native expo

No comments:

Post a Comment