最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass a Promise.all return to another function Cloud Functions for Firebase - Stack Overflow

programmeradmin0浏览0评论

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, however, I can't tell if foo and bar are being passed into then, and also Firebase is not properly scoped.

The Promise.all block is based on a tutorial found here: ;t=305s

exports.someFunction = 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()
      // Properly sets foo and bar
      // As far as I can tell foo and bar are not passed into 'then'
    }).then([foo, bar] => {
      return someModule.anotherFunction({
        "foo": foo,
        "bar": bar
      })
    })
  } else {
    console.log('Fail');
  }
});

How can I pass foo and bar into anotherFunction and set the response of that function to 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, however, I can't tell if foo and bar are being passed into then, and also Firebase is not properly scoped.

The Promise.all block is based on a tutorial found here: https://www.youtube./watch?v=NgZIb6Uwpjc&t=305s

exports.someFunction = 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()
      // Properly sets foo and bar
      // As far as I can tell foo and bar are not passed into 'then'
    }).then([foo, bar] => {
      return someModule.anotherFunction({
        "foo": foo,
        "bar": bar
      })
    })
  } else {
    console.log('Fail');
  }
});

How can I pass foo and bar into anotherFunction and set the response of that function to Firebase?

Share Improve this question edited May 15, 2017 at 7:32 KENdi 7,7792 gold badges18 silver badges31 bronze badges asked May 14, 2017 at 2:43 KVNAKVNA 9071 gold badge11 silver badges25 bronze badges 4
  • 1 your first .then returns nothing, so your second .then wont receive foo or bar ... try return [foo, bar] – Jaromanda X Commented May 14, 2017 at 2:49
  • actually, the line }).then([foo, bar] => { is irrelevant and incorrect syntaxt in your example - remove that line and celebrate the fact that foo and bar are defined – Jaromanda X Commented May 14, 2017 at 2:51
  • .then([foo, bar] => {}) should be .then(([foo, bar]) => {}) – guest271314 Commented May 14, 2017 at 2:53
  • @JaromandaX Yes, you addressed both issues at your Answer. – guest271314 Commented May 14, 2017 at 3:00
Add a ment  | 

2 Answers 2

Reset to default 6

Here's where you've gone wrong - see the ments in the code

return Promise.all([foo, bar]).then(results => {
  const foo = results[0].val()
  const bar = results[1].val()
  // you dont' return anything so, the following .then gets undefined argument
}).then([foo, bar] => {
  //    ^^^^^^^^^^^^^ invalid syntax, you need .then(([foo, bar]) =>
  return someModule.anotherFunction({
    "foo": foo,
    "bar": bar
  })

To simplify your code, just remove }).then([foo, bar] => { altogether!!

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 ...

But, if there's more to the actual code than you show, you can do

return Promise.all([foo, bar])
.then(results => results.map(result => result.val()))
.then(([foo, bar]) => someModule.anotherFunction({
    "foo": foo,
    "bar": bar
}))
.then ...

or

return Promise.all([foo, bar])
.then(([foo, bar]) => ([foo.val(), bar.val()]))
.then(([foo, bar]) => someModule.anotherFunction({
    "foo": foo,
    "bar": bar
}))
.then ...

in the first then add

return Promise.resolve([foo,bar]);

or (as per @Jaromanda X)

return [foo, bar];
发布评论

评论列表(0)

  1. 暂无评论