Saturday 15 April 2017

FireBase Functions .once('value') method returning undefined

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: enter image description here

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



via Steven W

No comments:

Post a Comment