SyntaxError:意外的令牌&lt;在JSON位于JSON.parse的位置1(<anonymous>)

时间:2018-06-04 19:04:48

标签: json

我遇到此错误,我不知道我的代码在哪里出现问题 错误说 未定义:2 SyntaxError:意外的令牌&lt;在位置1的JSON中     在JSON.parse()

我的代码,我不知道问题在哪里,任何人都可以帮助我

function sendTextMessage(recipientId, messageText) { 
let text = messageText.toLowerCase();
// Get Data From a Google Sheet
var google_sheet_json = "https://spreadsheets.google.com/feeds/list/" + 
process.env.GOOGLE_SHEET_ID +"/od6/public/values?alt=json";
request.get(google_sheet_json, function (err, res, body) {
 if (!err) {
  var bot_script_obj = JSON.parse(body);
  var all_bot_scripts = bot_script_obj.feed.entry;

  var i = 0;
  var len = all_bot_scripts.length;
  var all_keywords = [];
  for (; i < len; ) {

    if (text.includes(bot_script_obj.feed.entry[i].gsx$incoming.$t)){
      var messageText = bot_script_obj.feed.entry[i].gsx$outgoing.$t;
      var messageData = {
        recipient: {
          id: recipientId
        },
        message: {
          text: messageText
        }
      };
      callSendAPI(messageData);
    }
    i++;
  }
} else {
  console.log(err);
    }
    });
  }

   function callSendAPI(messageData) {
   request({
    uri: 'https://graph.facebook.com/v2.6/me/messages',
    qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
     method: 'POST',
     json: messageData

   }, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var recipientId = body.recipient_id;
  var messageId = body.message_id;

  console.log("Successfully sent generic message with id %s to recipient %s", 
    messageId, recipientId);
} else {
  console.error("Unable to send message.");
  console.error(response);
  console.error(error);
  }
 });  
 }

1 个答案:

答案 0 :(得分:3)

似乎收到的body参数不是JSON而是HTML。