rajithShetty
rajithShetty

Reputation: 441

not getting string from the firebase database

    useEffect(()=>{

        fire.database().ref().child('/users/PKWNi2pt2mUfv097pTKwEtsnyOt2/preference/').
        on("value",(snapshot)=>{
            let item=snapshot.val()
            console.log(snapshot.val())
            setPrefer(item)
            }
    );})

[![][1]][1]

my db [1]: https://i.sstatic.net/3CvZT.png

I am expecting prefer to be

{"food":{"indian":false,"british":false,"american":false,
                                        "spanish":false,"chinese":false,"mexican":false,
                                        "japanese":false,"italian":false,"french":false}}

but I getting nothing. Possible error would be in the on function. Or function is working but giving me some other format of data, and need to convert it in some other format

Upvotes: 0

Views: 33

Answers (1)

Kirill Skomarovskiy
Kirill Skomarovskiy

Reputation: 1565

That's how I'd do it.

useEffect(()=>{
  const preferenceRef = fire.database().ref(
    '/users/PKWNi2pt2mUfv097pTKwEtsnyOt2/preference/'
  );
  cosnt callback = (snapshot) => {
    setPrefer(snapshot.val());
  };
  preferenceRef.on('value', callback);
  
  return () => {
    preferenceRef.off('value', callback);
  };
);
}, [])

Upvotes: 1

Related Questions