title: messenger bot: TypeError: Cannot read property 'forEach' of undefined
body: <p>I used node.js to implement a chat bot for facebook messenger. During testing I had no problems or errors. But after I uploaded the node.js webhook to a verified facebook developers app, I am getting the above error. Everything is working fine in my chatbot. People are getting replies from my dialogflow agent. But if i check my heroku logs i see "Cannot read property error". Why am i seeing this error when everything is working fine? During testing i did not get this error. I have checked other questions in stackoverflow aswell. I also updated everything using npm update, according to one of the answers in stackoverflow, but it did not help. Here is where my error originates from:</p>
  
  <pre><code>const dialogflow = require('dialogflow');
  const config = require('./config');
  const express = require('express');
  const crypto = require('crypto');
  const bodyParser = require('body-parser');
  const request = require('request');
  const app = express();
  const uuid = require('uuid');
  const pg = require('pg');
  pg.defaults.ssl = true;
  
  app.use(bodyParser.json({
    verify: verifyRequestSignature
  }));
  
  //serve static files in the public directory
  app.use(express.static('public'));
  
  // Process application/x-www-form-urlencoded
  app.use(bodyParser.urlencoded({
    extended: false
  }));
  
  // Process application/json
  app.use(bodyParser.json());
  
  app.post('/webhook/', function (req, res) {
    var data = req.body;
    console.log(JSON.stringify(data));
    // Make sure this is a page subscription
    if (data.object == 'page') {
      // Iterate over each entry
      // There may be multiple if batched
      data.entry.forEach(function (pageEntry) {
        var pageID = pageEntry.id;
        var timeOfEvent = pageEntry.time;
        console.log("pageentery:" + pageEntry);
        console.log("messaging:" + pageEntry.messaging);
        // Iterate over each messaging event
        pageEntry.messaging.forEach(function (messagingEvent) {
  
          if (messagingEvent.optin) {
            receivedAuthentication(messagingEvent);
          } else if (messagingEvent.message) {
            receivedMessage(messagingEvent);
          } else if (messagingEvent.delivery) {
            receivedDeliveryConfirmation(messagingEvent);
          } else if (messagingEvent.postback) {
            receivedPostback(messagingEvent);
          } else if (messagingEvent.read) {
            receivedMessageRead(messagingEvent);
          } else if (messagingEvent.account_linking) {
            receivedAccountLink(messagingEvent);
          } else {
            console.log("Webhook received unknown messagingEvent: ", messagingEvent);
          }
        });
      });
      // Assume all went well.
      // You must send back a 200, within 20 seconds
      res.sendStatus(200);
    }
  });
  </code></pre>
  
  <p>So, I am getting this error, "TypeError: Cannot read property 'forEach' of undefined" on pageEntry.messaging.forEach(function (messagingEvent) {..}. </p>