2017-09-05 167 views
0

無法讀取的未定義的屬性「數據」我有以下的雲功能:火力地堡雲功能 - 類型錯誤:當數據存在

exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onWrite(event => { 
    console.log("write on posts..."); 
    console.log(event.previous.params.postID); 
    console.log(event.data.previous.val()); 
    console.log(event.data.previous.val().postID); 
    var postKey = event.data.previous.val().postID; 


    // When a table, category, or region is changed the old upload has to be deleted 
    if (event.data.previous.exists()) { 
    if (event.data.previous.val().table != event.data.val().table || event.data.previous.val().region != 
     event.data.val().region || event.previous.data.val().category != event.data.val().category) { 
      // category, region, or table was changed 
      console.log(postKey); 
      if (event.data.previous.val().table != event.data.val().table) { 
      console.log("Table was changed"); 
      // delete the post from the old table 
      const oldTable = event.data.previous.val().table; 
      const newTable = event.data.val().table; 

      addToNewTable(newTable, postKey); 
      removePostFromOldTable(oldTable, postKey); 
      } 
      if (event.data.previous.val().category != event.data.val().category) { 
      console.log("Category was changed"); 
      // delete the post from the old category 
      const oldCategory = event.data.previous.val().category; 
      const newCategory = event.data.val().category; 

      addToNewCategory(newCategory, postKey); 
      removePostFromOldCategory(oldCategory, postKey); 
      } 
      if (event.data.previous.val().region != event.data.val().region) { 
      console.log("Region was changed"); 
      // delete post from old region 
      const oldRegion = event.data.previous.val().region; 
      const newRegion = event.data.val().region; 

      addToNewRegion(newRegion, postKey); 
      removePostFromOldRegion(oldRegion, postKey); 
      } 
     } 
     else { 
      return 
     } 
} 
else { 
    // previous value does not exist this case is handled by 
    // copyPostKey 
    return 
} 
}); 

當一個表或區域被改變它的工作完全正常,但無法每次類別被改變。錯誤來自行var postKey = event.data.previous.val().postID;這個值如何被讀取一些而不是其他的?我甚至可以通過控制檯登錄密鑰,但是當我嘗試將其分配給postKey時,它表示無法讀取密鑰。任何想法這個問題是從什麼?

的數據總是從我的iOS應用

ref.child("posts").child(editedPost.postID).updateChildValues(["table": editedPost.table]) 
ref.child("posts").child(editedPost.postID).updateChildValues(["category": editedPost.category]) 
ref.child("posts").child(editedPost.postID).updateChildValues(["region": editedPost.region]) 

節點= v6.11.2 火力工具編寫同樣的方式= 3.10.10

回答

0

除了馬茨回答,您需要調整您的邏輯。

exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onWrite(event => { 
    // When a table, category, or region is changed the old upload has to be deleted 
    if (event.data.previous.exists()) { 
     var postKey = event.data.previous.val().postID; 

如果您在event.data.previous.exists()語句後檢查postKey你不應該有任何問題。

它可能值得看看Firebase Documentation,它們有不同的觸發器,具體取決於寫入操作;

onWrite(),在實時數據庫中創建,銷燬或更改數據時觸發。

onCreate(),在實時數據庫中創建新數據時觸發。

onUpdate(),在實時數據庫中更新數據時觸發。

onDelete(),當從實時數據庫中刪除數據時觸發。

在你的情況,你可以改變你的邏輯使用onUpdate

exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onUpdate(event => { 
    var currentValue = currentValue; 
    var previousValue = previousValue; 
    var postKey = previousValue.postID; 


    // When a table, category, or region is changed the old upload has to be deleted 
    if (previousValue.table != currentValue.table || previousValue.region != currentValue.region || event.previous.data.val().category != currentValue.category) { 
     // category, region, or table was changed 
     console.log(postKey); 
     if (previousValue.table != currentValue.table) { 
     console.log("Table was changed"); 
     // delete the post from the old table 
     const oldTable = previousValue.table; 
     const newTable = currentValue.table; 

     addToNewTable(newTable, postKey); 
     removePostFromOldTable(oldTable, postKey); 
     } 
     if (previousValue.category != currentValue.category) { 
     console.log("Category was changed"); 
     // delete the post from the old category 
     const oldCategory = previousValue.category; 
     const newCategory = currentValue.category; 

     addToNewCategory(newCategory, postKey); 
     removePostFromOldCategory(oldCategory, postKey); 
     } 
     if (previousValue.region != currentValue.region) { 
     console.log("Region was changed"); 
     // delete post from old region 
     const oldRegion = previousValue.region; 
     const newRegion = currentValue.region; 

     addToNewRegion(newRegion, postKey); 
     removePostFromOldRegion(oldRegion, postKey); 
     } 
    } else { 
     return 
    } 
});