为什么在使用Promise.all时无法返回Promise中的snapshot.val()? [英] Why does returning snapshot.val() in a Promise when using Promise.all not work?

查看:179
本文介绍了为什么在使用Promise.all时无法返回Promise中的snapshot.val()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Firebase云端函数,我想知道 Promise.all 是如何工作的。在我的代码中,我传递了数组数据库查询,我正在尝试读取结果数组,但我只是得到垃圾:

  T {
A:
P {
k:Sb {Ka:[Function:vb],ba:[Object]},
aa:P {k:[Object],aa:null ,wb:[Object],Bb:''},
wb:Zc {ld:[Object],ac:[Object]},
Bb:null},
V:
R

Gd {
app:[Object],
L:[Object],
Ua:[Object],
Sc:null,
ca:[Object],
td:1,
Qa:[Object],
va:[Object],
qg:[Object ],
jc:[Object],
ee:[Object],
md:[Object],
ia:[Object],
Xa:[Object],
cd:2,
fe:null,
K:[Object]},
path:J {o:[Object],Y:0},
m:
Df {
xa:false,
ka:false,
Ib:false,
na:false,
Pb:false,
oa:0 ,
kb:'',
bc:null,
xb:'',
Zb:null,
vb:'',
g:Tc {}},
Kc:false,
then:undefined,
catch:undefined},$ b $:Tc {}}

我期待一个简单的json:

  { 
name:Foo,
number:2521
//和其他一些字段
}
pre>

顺便说一下,我观看了 Jen的视频所以我知道什么反正我做错了;我只是想知道为什么我现有的代码不起作用。 (我还没有测试过,但我相信解决方案是在我的数据库查询中返回原始快照,然后执行 .val()调用。)



相关代码如果链接消失:

 函数mergeTeams(duplicates){
return Promise.all([
admin.database()。ref(someRef).once('value',(snap)=> {
return snap.val(); $ b $($ b $)},
admin.database()。ref(someRef2).once('value',(snap)=> {
return snap.val();
})








$ team $ = 1];
console.log(team1);
console.log(team2);
}


$ div
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ > return Promise.all([
admin.database()。ref(teamRef + duplicates.teamK ('value'),
admin.database()。ref(teamRef + duplicates.teamKey2).once('value')
))。then(values => {
const team1 = values [0] .val();
const team2 = values [1] .val();

console.log(team1);
console.log(team2);
});

它的工作原因是因为我总是在数组,即使我不知道它。以下是 Promise.all 返回值:一个数组,其中传入了promise的原始结果。当我在成功回调,那实际上没有做任何事情,因为它不是承诺的一部分;我只是随机的东西回到空虚。当我打印团队时,实际上是记录Firebase Snapshot 对象而不是 .val()

I'm writing a Firebase Cloud Function and I'm trying to figure out how Promise.all works. In my code, I pass in an array of database queries and I'm trying the read the resulting array, but I'm only getting garbage:

T {
  A: 
   P {
     k: Sb { Ka: [Function: vb], ba: [Object] },
     aa: P { k: [Object], aa: null, wb: [Object], Bb: '' },
     wb: Zc { ld: [Object], ac: [Object] },
     Bb: null },
  V: 
   R {
     u: 
      Gd {
        app: [Object],
        L: [Object],
        Ua: [Object],
        Sc: null,
        ca: [Object],
        td: 1,
        Qa: [Object],
        va: [Object],
        qg: [Object],
        jc: [Object],
        ee: [Object],
        md: [Object],
        ia: [Object],
        Xa: [Object],
        cd: 2,
        fe: null,
        K: [Object] },
     path: J { o: [Object], Y: 0 },
     m: 
      Df {
        xa: false,
        ka: false,
        Ib: false,
        na: false,
        Pb: false,
        oa: 0,
        kb: '',
        bc: null,
        xb: '',
        Zb: null,
        vb: '',
        g: Tc {} },
     Kc: false,
     then: undefined,
     catch: undefined },
  g: Tc {} }

I'm expecting a simple json:

{
    "name": "Foo",
    "number": 2521
    // And a few other fields
}

BTW, I watched Jen's video so I know what I'm doing is wrong anyway; I just want to know why my existing code doesn't work. (I haven't tested it, but I believe the solution is to return the raw snapshots in my db query and then do the .val() call.)

Relevant code if the links disappear:

function mergeTeams(duplicates) {
    return Promise.all([
        admin.database().ref(someRef).once('value', (snap) => {
            return snap.val();
        }),
        admin.database().ref(someRef2).once('value', (snap) => {
            return snap.val();
        })
    ]).then(values => {
        console.log(values);

        const team1 = values[0];
        const team2 = values[1];
        console.log(team1);
        console.log(team2);
}

解决方案

So, here's the code that works (and the explanation below):

return Promise.all([
    admin.database().ref(teamRef + duplicates.teamKey1).once('value'),
    admin.database().ref(teamRef + duplicates.teamKey2).once('value')
]).then(values => {
    const team1 = values[0].val();
    const team2 = values[1].val();

    console.log(team1);
    console.log(team2);
});

The reason it works is because I've always getting the promises in the values array even though I didn't know it. Here's what Promise.all returns: an array with the raw result of the promises passed in. When I was returning stuff inside the success callback, that didn't actually do anything because it wasn't part of the promise; I was just returning random stuff to an empty void. And when I was printing the teams, I was actually logging the Firebase Snapshot object instead of the .val().

这篇关于为什么在使用Promise.all时无法返回Promise中的snapshot.val()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆