简体   繁体   中英

How to get response from firebase after set?

function createGame(gameId, pin, currentDateTime, playerName)
{
  firebase.database().ref('games/' + gameId).set({
    pin: pin,
    createTime: currentDateTime,
    started: "0"
  });

  firebase.database().ref('games/' + gameId + '/players').set({
    id: "1",
    name: playerName,
    createTime: currentDateTime,
    status: "1"
  });
}

How to get response whether the set was successful or not in javascript? Please help I searched all over the internet!

As you can see from the API documentation, set() returns a promise that resolves or rejects when the operation completes. This works just like any other promise in JavaScript.

For example:

  firebase.database().ref('games/' + gameId).set({
    pin: pin,
    createTime: currentDateTime,
    started: "0"
  })
  .then(() => {
    // success
  })
  .catch(error => {
    // error
    console.error(error);
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM