SyntaxError:无法解析JSON正文:意外的令牌o

时间:2013-03-27 13:56:18

标签: json node.js heroku

我在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.parse一样,即我添加了JSON.stringify和{{1}}来电,但没有任何变化。

1 个答案:

答案 0 :(得分:1)

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