2017-03-31 83 views
0

嗨,我想在Node.js環境中使用雲端功能爲Firebase訂閱特定主題的用戶,我已跟隨此視頻https://www.youtube.com/watch?v=GNR9El3XWYo,其中Firebase專家展示瞭如何訂閱用戶時,在火力地堡節點寫而成,here's我的完整代碼:Firebase的雲端功能 - 訂閱用戶到主題

//For subscriptions 
exports.privateEventSubs = functions.database.ref("Events/{eventID}/guest_mod/assistants/{guestEmail}").onWrite((event) => { 
    let data = event.data; 
    let eventID = data.ref.parent.parent.parent.key; 
    console.log("The eventID is: " + eventID); 
    let guestEmail = data.ref.key; 
    console.log("The guestEmail is: " + guestEmail); 

    //Add or remove a subscription 
    let action = data.exists() ? "batchAdd" : "batchRemove"; 
    console.log("The action is: " + action); 


    //Get the device token from each user 
    admin.database().ref("Users/" + guestEmail + "/chat_data/firebaseToken").once("value").then(function (snapshot) { 
     console.log("The user token is: " + snapshot.val()); 

     //Send messages to users that have subscribed to that event 
     functions.config().firebase.credential.getAccessToken().then(function (oauthToken) { 
      console.log("The oauthToken is: " + oauthToken.access_token); 
      //Lets configure and request 
      request({ 
       url: "https://iid.googleapis.com/iid/v1:" + action, //URL to hit 
       method: 'POST', 
       json: true, 
       headers: { 
        Authorization: "Bearer " + oauthToken.access_token, 
        access_token_auth: true, 
       }, 
       body: { 
        to: "/Events/" + eventID, 
        registration_tokens: snapshot.val() 
       } 
      }, function (err, res, body) { 
       if (err) { 
        console.log(err); 
       } else { 
        console.log(res.statusCode, body); 
       } 
      }); 
     }); 
    }); 
}); 

我得到的錯誤是這個:

400 { error: 'InvalidListOfTokens' } 

我在做什麼錯誤?請幫忙! :(

+0

問題和錯誤已更新! – Andrea

回答

0

在這種情況下,它看起來像你傳遞的registration_tokens鍵的單一值。按照API documentation這應該是一個數組。

因此改變registration_tokens: snapshot.val()registration_tokens: [snapshot.val()]應該把該請求的格式API期望