2017-04-15 102 views
0

目前我已經開始爲我的項目使用Firebase函數。我在GitHub上查看了示例項目,並在聲明管理員引用並將其返回到承諾中時看到一些示例項目使用.once("value")。我的問題是這樣的:FireBase函數.once('value')方法返回undefined

我的代碼:

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

回答

0

做一些研究,並通過我發現我需要做recipient_name.val示例程序讀透後()返回裁判的價值