节点js:无法读取未定义的属性“文本”

时间:2018-06-28 22:00:13

标签: javascript node.js express facebook-messenger facebook-messenger-bot

我正在用node js express构建一个Messenger机器人,我正在尝试将index.js文件拆分为两个文件。这是msg.js的代码,这是新文件

'const
  express = require('express'),
  bodyParser = require('body-parser'),
  request = require('request'),
  PAGE_ACCESS_TOKEN ="",
  app = express().use(bodyParser.json());
//functions
module.exports = {
//hangles messages
 handleMessage:function (sender_psid, received_message) {
  let response;

  // Checks if the message contains text
  if (received_message.text) {
    // Create the payload for a basic text message, which
    // will be added to the body of our request to the Send API
    response = {
      "text": `You sent the message: "${received_message.text}". Now send me an attachment!`
    }
  } else if (received_message.attachments) {
    // Get the URL of the message attachment
    let attachment_url = received_message.attachments[0].payload.url;
    response = {
      "attachment": {
        "type": "template",
        "payload": {
          "template_type": "generic",
          "elements": [{
            "title": "Is this the right picture?",
            "subtitle": "Tap a button to answer.",
            "image_url": attachment_url,
            "buttons": [
              {
                "type": "postback",
                "title": "Yes!",
                "payload": "yes",
              },
              {
                "type": "postback",
                "title": "No!",
                "payload": "no",
              }
            ],
          }]
        }
      }
    }
  }

  // Send the response message
  module.exports.callSendAPI(sender_psid, response);
},

// Handles messaging_postbacks events
  handlePostback:function (sender_psid, received_postback) {
  let response;
  // Get the payload for the postback
  if (received_postback) {
    let payload = received_postback.payload;
  }
  // Send the message to acknowledge the postback
  module.exports.callSendAPI(sender_psid, response);
},

  // Sends response messages via the Send API
callSendAPI:function (sender_psid, response) {
  // Construct the message body
  let request_body = {
    "recipient": {
      "id": sender_psid
    },
    "message": response
  }

  // Send the HTTP request to the Messenger Platform
  request({
    "uri": "https://graph.facebook.com/v2.6/me/messages",
    "qs": { "access_token": PAGE_ACCESS_TOKEN },
    "method": "POST",
    "json": request_body
  }, (err, res, body) => {
    if (!err) {
      console.log('message sent!')
    } else {
      console.error("Unable to send message:" + err);
    }
  });
  }
};

在index.js文件的底部有以下代码。

//Imports functions from other files
let  msg = require('./msg.js'),
     handleMessage = msg.handleMessage(),
     handlePostback = msg.handlePostback(),
     callSendAPI = msg.callSendAPI();

我遇到以下错误:

msg.js:14   如果(received_message.text){                        ^

TypeError:无法读取未定义的属性“文本”

1 个答案:

答案 0 :(得分:2)

问题是这一行:

if (received_message.text) {

调用此函数时,传入的received_message是不确定的,因此,当您尝试从text变量获取received_message字段时,由于{{1 }}是未定义的,因此将没有可以从中调用的任何字段。在传递给handleMessage函数之前,请检查是否已正确设置了receive_message。