title: Firebase Cloud Functions - TypeError: Cannot read property 'data' of undefined when data exists
body: <p>I have the following cloud function:</p>
  
  <pre><code>exports.keepPostKeysUpdated = functions.database.ref('/posts/{postid}').onWrite(event =&gt; { 
    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
  }
  });
  </code></pre>
  
  <p>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 <code>var postKey = event.data.previous.val().postID;</code> 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?</p>
  
  <p>The data is always written the same way from my iOS app</p>
  
  <pre><code>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])
  </code></pre>
  
  <p>node = v6.11.2
  firebase-tools = 3.10.10</p>