Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase push promise never resolves

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:

My Firebase DB Rules

Here's my Firebase realtime DB view:

Firebase realtime DB view

Question- I am pushing to /users/${currentUser.uid}/employees path, do I need to manually create 'users' node?

like image 208
user655577 Avatar asked Apr 25 '18 20:04

user655577


2 Answers

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);
  });
like image 52
lesimoes Avatar answered Oct 02 '22 03:10

lesimoes


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.

like image 37
Ankur Avatar answered Oct 02 '22 04:10

Ankur