javascript - Firebase 云函数 - TypeError : Cannot read property 'data' of undefined when data exists

标签 javascript firebase google-cloud-functions

我有以下云功能:

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; 如何有时读取此值但有时不读取?我什至可以控制台记录 key ,但它说当我尝试将其分配给 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 firebase 工具 = 3.10.10

最佳答案

previous.val() 只有在前一个值存在时才有效。如果这是该路径的第一次写入,它可能不存在。在引用它之前,您应该检查它是否存在:

event.data.previous.exists()

https://firebase.google.com/docs/database/extend-with-functions#reading_the_previous_value

关于javascript - Firebase 云函数 - TypeError : Cannot read property 'data' of undefined when data exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063941/

相关文章:

javascript - 尝试从 opentable 获取 JSON 并输出结果

javascript - jQuery 切换菜单,在外部单击时隐藏下拉内容

ios - 如何在 Firebase IOS 中搜索值

node.js - 如何从 firebase 函数中的 "get request"获取参数?

Firebase 存储功能权限

javascript - 如何嵌入 YouTube iframe 视频 100% 全宽

javascript - 在 WordPress 中包含包含 Php 的 JavaScript

reactjs - Firebase 存储 "Permission denied"错误

firebase - Flutter Firebase 存储视频旋转

function - firebase deploy --only 函数覆盖现有函数