Javascript 如何传递承诺。全部返回到另一个函数Firebase的云函数

Javascript 如何传递承诺。全部返回到另一个函数Firebase的云函数,javascript,node.js,firebase,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Functions,我试图从Firebase中设置一些变量,然后将它们传递到另一个函数中。目前,Promise.all正在正确设置foo和bar,但是,我无法判断foo和bar是否正在传递到中,然后,而且Firebase的范围也没有正确确定 Promise.all块基于以下教程: 如何将foo和bar传递到另一个函数中,并将该函数的响应设置为Firebase?首先然后 加 或(根据@Jaromanda X) 这里是您出错的地方-请参阅代码中的注释 return Promise.all([foo, bar]).the

我试图从Firebase中设置一些变量,然后将它们传递到另一个函数中。目前,
Promise.all
正在正确设置
foo
bar
,但是,我无法判断
foo
bar
是否正在传递到
中,然后
,而且Firebase的范围也没有正确确定

Promise.all
块基于以下教程:


如何将
foo
bar
传递到
另一个函数中
,并将该函数的响应设置为Firebase?

首先
然后

或(根据@Jaromanda X)


这里是您出错的地方-请参阅代码中的注释

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
  })
为了简化您的代码,只需删除
})。然后([foo,bar]=>{
一起

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 ...
但是,如果实际代码中有比您显示的更多的内容,您可以这样做

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


你的第一个。然后什么也不返回,所以你的第二个。然后不会收到foo或bar…尝试
返回[foo,bar]
实际上,行
})。然后([foo,bar]=>{
在你的示例中是不相关和不正确的语法-删除该行并庆祝foo和bar是定义的
。然后([foo,bar]=>{})
应该是
。然后(([foo,bar])=>{}
@JaromandaX是的,您在回答时解决了这两个问题。或者只是
返回[foo,bar]
-因为它在一个中。那么,你知道这个简化的代码解决方案似乎一直在工作,直到因为没有定义Firebase而抛出错误。我不明白你的意思-没有定义Firebase更多的是与你的原始代码有关,它似乎根本没有引用Firebase。我会清理这个提交,使其更具承诺性fic,并使用(希望)更干净的代码分别重新提交Firebase问题。如果您想查看以下链接:
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
  })
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 ...
return Promise.all([foo, bar])
.then(results => results.map(result => result.val()))
.then(([foo, bar]) => someModule.anotherFunction({
    "foo": foo,
    "bar": bar
}))
.then ...
return Promise.all([foo, bar])
.then(([foo, bar]) => ([foo.val(), bar.val()]))
.then(([foo, bar]) => someModule.anotherFunction({
    "foo": foo,
    "bar": bar
}))
.then ...