Messenger机器人:TypeError:无法读取未定义的属性'forEach'

问题描述 投票:0回答:1

我使用node.js为Facebook Messenger实施了聊天机器人。在测试期间,我没有任何问题或错误。但是,在将node.js webhook上传到经过验证的Facebook开发人员应用程序后,出现了以上错误。在我的聊天机器人中,一切正常。人们从我的dialogflow代理那里得到答复。但是,如果我检查heroku日志,则会看到“无法读取属性错误”。当一切正常时,为什么会看到此错误?在测试过程中,我没有得到这个错误。我也检查了stackoverflow中的其他问题。根据stackoverflow中的答案之一,我还使用npm update更新了所有内容,但没有帮助。这是我的错误来源:

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);
  }
});
<<

我使用node.js为Facebook Messenger实施了聊天机器人。在测试期间,我没有任何问题或错误。但是,当我将node.js webhook上传到经过验证的Facebook开发人员应用后,我得到了...

node.js express dialogflow facebook-messenger-bot body-parser
1个回答
0
投票
正确处理空和未定义的情况
© www.soinside.com 2019 - 2024. All rights reserved.