FireBase函数.once('value')方法返回undefined

时间:2017-04-15 21:11:59

标签: node.js function firebase

目前我已经开始为我的项目使用firebase功能。我在GitHub上看了一些示例项目,看到一些示例项目在声明管理员引用并在promises中返回时使用.once("value")。我的问题如下:

我的代码:

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

执行console.log(recipient_name); 返回以下输出,如图像中所示:

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 {} }

1 个答案:

答案 0 :(得分:0)

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