SyntaxError:邮政快递路线中JSON输入的意外结束

时间:2019-05-25 13:41:09

标签: node.js express

我正在用路由的post方法写出一些更新的JSON内容,但是在过程完成时遇到SyntaxError: Unexpected end of JSON input错误消息。 JSON文件确实已更新,但它使服务器崩溃。我猜想generateHTML操作尚未完成或导致其他问题。

我已经验证了进入fs.writeFile方法的JSON是否有效。

...
var page = {"json": "data"};
fs.writeFile('files/page.json', JSON.stringify(page), (err) => {
  if (err) {
    console.log(err);
  }
  res.render('page', {output: generateHTML(page)});    
});

function generateHTML(item) {
  var file = loadJSON('files/' + item + '.json');
  ...
  // Generate HTML content and return it to the variable to serve to targetPage.ejs
  ...
}

function loadJSON(json) {
  var file = fs.readFileSync(json, 'utf8');
  return JSON.parse(file);
}

完整错误消息:

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at loadJSON (/Users/foobar/web/application/app.js:337:17)
    at /Users/foobar/web/application/app.js:251:23
    at Layer.handle [as handle_request] (/Users/foobar/web/application/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/foobar/web/application/node_modules/express/lib/router/route.js:137:13)
    at next (/Users/foobar/web/application/node_modules/express/lib/router/route.js:131:14)
    at Route.dispatch (/Users/foobar/web/application/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/foobar/web/application/node_modules/express/lib/router/layer.js:95:5)
    at /Users/foobar/web/application/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/foobar/web/application/node_modules/express/lib/router/index.js:335:12)
/Users/foobar/web/application/node_modules/express/lib/response.js:966
    if (err) return req.next(err);
                        ^

TypeError: req.next is not a function
    at done (/Users/foobar/web/application/node_modules/express/lib/response.js:966:25)
    at tryRender (/Users/foobar/web/application/node_modules/express/lib/application.js:642:5)
    at Function.render (/Users/foobar/web/application/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/foobar/web/application/node_modules/express/lib/response.js:971:7)
    at fs.writeFile (/Users/foobar/web/application/app.js:214:25)
    at FSReqWrap.oncomplete (fs.js:141:20)

0 个答案:

没有答案