Firebase云功能 - TypeError:无法读取属性'数据'数据存在时未定义的

时间:2017-09-05 22:01:02

标签: 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;行。如何读取此值有时却不读取其他值?我甚至可以控制登录密钥,但是当我尝试将其分配给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])

node = v6.11.2 firebase-tools = 3.10.10

2 个答案:

答案 0 :(得分:1)

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

event.data.previous.exists()

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

答案 1 :(得分:0)

除了Matts的答案,你需要调整你的逻辑。

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;

如果您在postKey声明后检查event.data.previous.exists(),则不应该有任何问题。

可能值得查看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
    }
});