when i fetch the data from firebase real time database, i am getting string object like below. May i know how can i convert this to JSON object?. how can i get the data like Address:"Add line 1, Add line 2, Street,City,State.". ?. This is for my react native app.
What i tried:
- when i tried to use JSON.parse, i am getting the error ‘JSON Parse
error: Unexpected token: o’. - when i display the data, it is [object object]
- when i try typeof, i am getting result as object
data that i get from Firebase real time database(It is getting displayed like below when i use JSON.stringify):
user:{"Address":"Add line 1, Add line 2, Street,City,State.","Age":"42","CAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"C1","Slot":"","Treatment":"","extra1":"","extra2":""}},"PAppoint":{"0":{"AppointDate":"","ConsultDoc":"","Patient":"P1","Slot":"","Treatment":"","extra1":"","extra2":""},"1":{"AppointDate":"","ConsultDoc":"","Patient":"P2","Slot":"","Treatment":"","extra1":"","extra2":""},"2":{"AppointDate":"","ConsultDoc":"","Patient":"P3","Slot":"","Treatment":"","extra1":"","extra2":""},"3":{"AppointDate":"","ConsultDoc":"","Patient":"P4","Slot":"","Treatment":"","extra1":"","extra2":""}},"name":"Zulu","pass":"1234","phone":"+918888888888"}
how i fetch data from firebase:
const readUserData = async (phoneNumber) => {
try {
const reference = ref(db, 'x123456/' + phoneNumber);
return (await get(reference)).toJSON((snapshot) => {
const data = snapshot.val();
console.log('DATA: ' + data);
}, (error) => {
console.error(error);
return 0;
});
}
How i call the above function:
exist=await readUserData('+91'+phoneNumber);
console.log('user:'+ JSON.stringify(exist));
} catch (error) {
alert(error);
}```
2
Answers
Maybe this is the answer you want :
or..
JSON required start with ‘{‘ and end with ‘}’.
(also required ‘key’ enclosed in quotation marks, like "user")
Your function return is an object already.
So simply you can use your desired property:
More Info