Saturday 13 May 2017

How to scope Firebase in nested promise Cloud Functions (Firebase)

I am trying to set a few variables from Firebase and then pass those into a anotherfunction. Currently, the Promise.all is properly setting foo and bar, but an error is being thrown during the .then, so the response isn't getting set in Firebase.

exports.someFunc = functions.database.ref(`/data`).onWrite(event => {
  const condition = event.data.val()
  if (condition) {
    // Get the data at this location only once, returns a promise, to ensure retrieval of foo and bar
    const foo = event.data.adminRef.child('foo').once('value')
    const bar = event.data.adminRef.child('bar').once('value')

    return Promise.all([foo, bar]).then(results => {
      const foo = results[0].val()
      const bar = results[1].val()

      return someModule.anotherFunction({
        "foo": foo,
        "bar": bar
      }).then(response => {
        // Get an error thrown that Firebase is not defined
        console.log(response);
        let updates = {};
        updates['/data'] = response;
        return firebase.database().ref().update(updates);
      })
    })
  } else {
    console.log('Fail');
  }
});

How can return firebase.database().ref().update(updates); be scoped to set the response from anotherFunction?



via KVNA

No comments:

Post a Comment