Firebase Cloud Functions-TypeError:数据存在时无法读取未定义的属性"data" [英] Firebase Cloud Functions - TypeError: Cannot read property 'data' of undefined when data exists

查看:50
本文介绍了Firebase Cloud Functions-TypeError:数据存在时无法读取未定义的属性"data"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下云功能:

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时无法读取.知道这个问题是什么原因吗?

It work perfectly fine when a table or region is changed but fails every time a category is changed. The error is coming from the line var postKey = event.data.previous.val().postID; How can this value be read some times but not others? I can even console log the key, but it says it can't be read when I try to assign it to postKey. Any idea what this issue is from?

数据始终以相同的方式从我的iOS应用中写入

The data is always written the same way from my iOS app

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

node = v6.11.2 firebase-tools = 3.10.10

推荐答案

除了Matts答案外,您还需要调整逻辑.

In addition to Matts answer, you need to adjust your logic.

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,则不会有任何问题.

If you check the postKey after the event.data.previous.exists() statement you shouldn't have any problems.

可能值得看一下 Firebase文档,它们有不同之处触发取决于写操作;

It might be worth looking at the Firebase Documentation, they have different triggers depending on the write operation;

onWrite(),在实时数据库中创建,销毁或更改数据时触发.

onWrite(), which triggers when data is created, destroyed, or changed in the Realtime Database.

onCreate(),在实时数据库中创建新数据时触发.

onCreate(), which triggers when new data is created in the Realtime Database.

onUpdate(),在实时数据库中更新数据时触发.

onUpdate(), which triggers when data is updated in the Realtime Database.

onDelete(),当从实时数据库中删除数据时触发.

onDelete(), which triggers when data is deleted from the Realtime Database.

根据您的情况,您可以更改逻辑以使用onUpdate

In your case, you could change your logic to use 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
    }
});

这篇关于Firebase Cloud Functions-TypeError:数据存在时无法读取未定义的属性"data"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆