FireBase函数.once('value')方法返回undefined [英] FireBase Functions .once('value') method returning undefined

查看:179
本文介绍了FireBase函数.once('value')方法返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我已经开始为我的项目使用Firebase功能。我查看了GitHub上的示例项目,看到一些示例项目使用 .once(value)来声明管理员引用并将其返回到promise中。我的问题是:

我的代码:

 出口。 HandleNotifications = functions.database.ref('/ users / {user_id} / Friends / pending / {recipient_id}')。onWrite(event => {

if(!event.data.exists( )){
return null;
}
console.log(event.data.val());
let recipient_id = event.data.val();
get getipipientProfile = admin.auth()。getUser(recipient_id);
getDeviceTokens = admin.database()。ref('users /'+ event.params.user_id +'/ deviceTokens')。once ');
let getRecipientName = admin.database()。ref(`/ users / $ {event.params.recipient_id} / displayName`).once('value');

返回Promise.all([getDeviceTokens,getrecipientProfile,getRecipientName])。then(others => {

let tokensSnapshot = others [0];
let recipient = others [1];
让recipient_name = other [2];
console.log(recipient_name);

//检查是否有任何设备令牌。
if(!tokensSnapshot.hasChildren()){
return console.log('没有通知标记发送。');
}
console.log('There',tokensSnapshot.numChildren(),'tokens to send notifications to。');读亦信息亦会读亦预亦辛

//通知详情。
const payload = {
notification:{
title:'你有一个新的朋友请求',
body:recipient.displayName +`请求跟随你.`,
icon:recipient.photoURL
}
};

//列出所有的标记。
const tokens = Object.keys(tokensSnapshot.val());
返回admin.messaging()。sendToDevice(tokens,payload);

});
});

输出



执行console.log(recipient_name);
返回如下图所示的输出:

  T {
A:
Rb {
B:'StevenWong',
aa:P {k:[Object],aa:null,wb:[Object],Bb:''},
Bb:null},
V
$ {
u:
Gd {
app:[Object],
L:[Object],
Ua:[对象],
Sc:null,
ca:[Object],
td:1,
Qa:[Object],
va:[Object],
qg:[Object],
jc:[Object],
ee:[Object],
md:[Object],
ia:[Object],
Xa:[Object],
cd:1,
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,
'',
g:Tc {}},
Kc:false,
then:undefined,
catch:undefined },$ b $:Tc {}}


解决方案

通过示例程序进行了一些研究和彻底的阅读之后,我发现我需要执行recipient_name.val()来返回ref的值

Currently I have started using firebase functions for my project. I went through the sample projects on GitHub and saw some of the sample projects use .once("value") when declaring admin references and returning them in the promises. My problem is the following:

my code:

exports.HandleNotifications = functions.database.ref('/users/{user_id}/Friends/pending/{recipient_id}').onWrite(event => {

    if(!event.data.exists()){
        return null;
    }
    console.log(event.data.val());
    let recipient_id = event.data.val();
    let getrecipientProfile = admin.auth().getUser(recipient_id);
    let getDeviceTokens  = admin.database().ref('users/'+event.params.user_id+'/deviceTokens').once('value');
    let getRecipientName = admin.database().ref(`/users/${event.params.recipient_id}/displayName`).once('value');

    return Promise.all([getDeviceTokens,getrecipientProfile,getRecipientName]).then(others => {

        let tokensSnapshot = others[0];
        let recipient = others[1];
        let recipient_name = others[2];
        console.log(recipient_name);

        // Check if there are any device tokens.
        if (!tokensSnapshot.hasChildren()) {
            return console.log('There are no notification tokens to send to.');
        }
        console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
        console.log('Fetched follower profile', recipient);

        // Notification details.
        const payload = {
            notification: {
                title: 'You have a new Friend Request!',
                body:   recipient.displayName + `requested to follow you.`,
                icon:   recipient.photoURL
            }
        };

        // Listing all tokens.
        const tokens = Object.keys(tokensSnapshot.val());
        return admin.messaging().sendToDevice(tokens,payload);

    });
});

output

so executing console.log(recipient_name); returns the following output like in the image:

T {
  A: 
   Rb {
     B: 'StevenWong',
     aa: P { k: [Object], aa: null, wb: [Object], Bb: '' },
     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: 1,
        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,
   '',
        g: Tc {} },
     Kc: false,
     then: undefined,
     catch: undefined },
  g: Tc {} }

解决方案

After doing some research and reading thoroughly through the sample programs I found out that I need to do recipient_name.val() to return the value of the ref

这篇关于FireBase函数.once('value')方法返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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