I am trying to save an object from my React Native App. Please look at the below code snippet.
const { currentUser } = firebase.auth();
firebase.database().ref(`/users/${currentUser.uid}/employees`)
.push({ name, phone, shift })
.then(() => {
console.log('Save to Firebase was successful');
})
.catch((error) => {
console.log(error);
});
But in the Firebase console, I don't see anything. I am surprised that the then and catch don't even get called. What am I missing? The console does not show any error.
My Firebase DB Rules:
Here's my Firebase realtime DB view:
Question- I am pushing to /users/${currentUser.uid}/employees
path, do I need to manually create 'users' node?
In Firebase push() function genrates a unique key for each new child, and set() or update() functions insert or update data in node.
Try this:
firebase.database().ref(`/users/${currentUser.uid}/employees`)
.push()
.set({ name, phone, shift })
.then(() => {
console.log('Save to Firebase was successful');
})
.catch((error) => {
console.log(error);
});
Or this:
let niceKey = firebase.database().ref(`/users/${currentUser.uid}/employees`).push()
niceKey.set({ name, phone, shift })
.then(() => {
console.log('Save to Firebase was successful');
})
.catch((error) => {
console.log(error);
});
Don't use push to save data to Firebase, instead use set
or update
. Here's how it works:-
firebase.database().ref(`/users/${currentUser.uid}/employees`).set({
name: name,
phone: phone,
shift : shift
});
For more info visit:- https://firebase.google.com/docs/database/web/read-and-write
Hope it'll help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With