首页 > 解决方案 > SyntaxError:JSON.parse 中位置 1 的 JSON 中的意外标记 <)

问题描述

我正面临这个错误,我不知道我的代码的问题出在哪里,错误说 undefined:2 SyntaxError: Unexpected token < in JSON at position 1 at 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);
  }
 });  
 }

标签: json

解决方案


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


推荐阅读