【问题标题】:SyntaxError: Failed to parse JSON body: Unexpected token oSyntaxError:无法解析 JSON 正文:意外的令牌 o
【发布时间】:2013-04-04 03:33:54
【问题描述】:

我在 Facebook 上有一个 Node.js Heroku 应用程序,并且我不断在日志中发现以下错误(当我实际尝试访问我的应用程序时出现一般应用程序错误):

2013-03-27T12:58:54+00:00 heroku[web.1]: Starting process with command `node web
.js`
2013-03-27T12:58:55+00:00 app[web.1]: info: socket.io started
2013-03-27T12:58:55+00:00 app[web.1]: Listening on 6925
2013-03-27T12:58:56+00:00 heroku[web.1]: State changed from starting to up
2013-03-27T13:00:40+00:00 app[web.1]: ^
2013-03-27T13:00:40+00:00 app[web.1]: undefined:1
2013-03-27T13:00:40+00:00 app[web.1]:
2013-03-27T13:00:40+00:00 app[web.1]: SyntaxError: Failed to parse JSON body: Un
expected token o
2013-03-27T13:00:40+00:00 app[web.1]:     at Object.parse (native)
2013-03-27T13:00:40+00:00 app[web.1]:     at EventEmitter.emit (events.js:99:17)

2013-03-27T13:00:40+00:00 app[web.1]: SyntaxError: Unexpected token S
2013-03-27T13:00:40+00:00 app[web.1]:     at EventEmitter.mixin._fireError (/app
/node_modules/faceplate/node_modules/restler/lib/restler.js:192:10)
2013-03-27T13:00:40+00:00 app[web.1]:     at IncomingMessage.parsers.json (/app/
node_modules/faceplate/node_modules/restler/lib/restler.js:367:9)
2013-03-27T13:00:40+00:00 app[web.1]:     at EventEmitter.FaceplateSession.get (
/app/node_modules/faceplate/index.js:121:25)
2013-03-27T13:00:40+00:00 app[web.1]:     at mixin._responseHandler (/app/node_m
odules/faceplate/node_modules/restler/lib/restler.js:142:20)
2013-03-27T13:00:40+00:00 app[web.1]:     at EventEmitter.mixin._encode (/app/no
de_modules/faceplate/node_modules/restler/lib/restler.js:184:29)
2013-03-27T13:00:40+00:00 app[web.1]:     at IncomingMessage.parsers.auto (/app/
node_modules/faceplate/node_modules/restler/lib/restler.js:356:21)
2013-03-27T13:00:40+00:00 app[web.1]:     at mixin._responseHandler (/app/node_m
odules/faceplate/node_modules/restler/lib/restler.js:140:16)
2013-03-27T13:00:40+00:00 app[web.1]:     at EventEmitter.mixin._decode (/app/no
de_modules/faceplate/node_modules/restler/lib/restler.js:156:7)
2013-03-27T13:00:41+00:00 heroku[web.1]: Process exited with status 1
2013-03-27T13:00:41+00:00 heroku[web.1]: State changed from up to crashed

在我的服务器代码中,我使用JSON.stringify,例如console.log(data + ': is in the tags table, with tag_id=' + JSON.stringify(result)); 其中result 是从查询返回到 Postgres 数据库的答案。但是,我从不使用JSON.parse,所以这个错误有点令人困惑,而且我直到今天才遇到这个错误(我的应用程序在周一运行良好)。

同样令人困惑的是,如果我注释掉 JSON.stringify 调用,我仍然会收到错误消息,因此非常感谢任何想法!

根据https://github.com/heroku/faceplate/issues/26,我对我的代码进行了以下更改:

function handle_facebook_request(req, res) {

  // if the user is logged in
  if (req.facebook.token) {

 async.parallel([
  function(cb) {
    // query 4 friends and send them to the socket for this socket id
    req.facebook.get('/me/friends', { limit: 4 }, function(friends) {
      req.friends = JSON.parse(JSON.stringify(friends));
      cb();
    });
  },
  function(cb) {
    // query 16 photos and send them to the socket for this socket id
    req.facebook.get('/me/photos', { limit: 16 }, function(photos) {
      req.photos = JSON.parse(JSON.stringify(photos));
      cb();
    });
  },
  function(cb) {
    // query 4 likes and send them to the socket for this socket id
    req.facebook.get('/me/likes', { limit: 4 }, function(likes) {
      req.likes = JSON.parse(JSON.stringify(likes));
      cb();
    });
  },
  function(cb) {
    // use fql to get a list of my friends that are using this app
    req.facebook.fql('SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1', function(result) {
      req.friends_using_app = JSON.parse(JSON.stringify(result));
      cb();
    });
  }
], function() {
  render_page(req, res);
});

  } else {
    render_page(req, res);
  }
}

每一行都带有req.????,例如req.friends 以前就像 req.friends = friends; 一样,即我添加了 JSON.parseJSON.stringify 调用,但没有任何改变。

【问题讨论】:

  • 你能把字符串结果贴出来吗
  • 这可能是由于restler中的错误,请参阅此处github.com/heroku/faceplate/issues/26
  • @user568109, client.query('SELECT tag_id FROM tags WHERE tag_text = \'' + data + '\';', function(err, result) { 是我们得到结果的那一行。我在问题中给出的示例只是我使用JSON.stringify() 的多次之一,而且我之前没有遇到过问题。至于实际结果是什么,它不是字符串,我按照示例here 使用JSON.stringify()。另外,我没有使用restler。
  • 一周前我的应用程序运行时遇到了同样的错误。顺便说一句,面板正在使用restler,而您正在使用面板。
  • 是的,我使用的是 Node.js,并且我使用@user568109 发布的链接中提供的解决方案让我的应用程序正常工作。 (github.com/heroku/faceplate/issues/26#issuecomment-15564948)

标签: json node.js heroku


【解决方案1】:

如前所述,可以在此处找到解决方案:https://github.com/heroku/faceplate/issues/26 应该注意的是,现在有更多关于修复 0.0.4 版面板的详细信息(感谢我的提示!)

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 1970-01-01
    • 2017-10-13
    • 2013-03-15
    • 2012-09-21
    • 2016-12-18
    • 2012-11-07
    相关资源
    最近更新 更多