title: Cloud Function for Firebase Type Error - Cannot Read Property
body: <p>I'm trying to write a Cloud Function that creates a record whenever someone uses our legacy app to create a record (we have changed the Firebase backend architecture and want to slowly migrate users). However, I'm getting the following error in my logs:</p>
  
  <pre><code>TypeError: Cannot read property 'update' of undefined
      at exports.makeNewComment.functions.database.ref.onWrite.event (/user_code/index.js:14:92)
      at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
      at process._tickDomainCallback (internal/process/next_tick.js:129:7)
  </code></pre>
  
  <p>Here is the script in question:</p>
  
  <pre><code>//required modules
  var functions = require('firebase-functions');
  const admin = require('firebase-admin');
  
  // Listens for new comments added to /comments/ and adds it to /post-comments/
  
  exports.makeNewComment = functions.database.ref('comments/{commentId}').onWrite(event =&gt; {
    // Grab the current value of what was written to the Realtime Database.
    const commentId = event.params.commentId;
    const comment = event.data.val();
    // You must return a Promise when performing asynchronous tasks inside a Functions such as
    // writing to the Firebase Realtime Database.
    //return event.data.ref.parent.child('post-comments').set(comment);
    return functions.database.ref('post-comments/' + comment['postID'] + '/' + commentId).update(comment).then(url =&gt; {
      return functions.database.ref('user-comments/' + comment['postedBy'] + '/' + commentId).update(comment);
    });
  });
  
  //initialize
  admin.initializeApp(functions.config().firebase);
  </code></pre>
  
  <p>Thanks!</p>