Node.js SyntaxError:JSON中位于位置0的意外标记u
我正在尝试导出在postman上运行的收集产生的数据。我严格遵循这篇文章的步骤: 但是,这里提供的脚本肯定存在一些问题,因为每次我在node.js上运行它时,都会出现以下错误,这也是在通过postman发出post请求时发生的:Node.js SyntaxError:JSON中位于位置0的意外标记u,node.js,visual-studio-code,Node.js,Visual Studio Code,我正在尝试导出在postman上运行的收集产生的数据。我严格遵循这篇文章的步骤: 但是,这里提供的脚本肯定存在一些问题,因为每次我在node.js上运行它时,都会出现以下错误,这也是在通过postman发出post请求时发生的: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Error</titl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token u in JSON at position 0
<br> at JSON.parse (<anonymous>)
<br> at C:\Users\Pablo\Node\hlr.js:13:58
<br> at Layer.handle [as handle_request] (C:\Users\Pablo\Node\node_modules\express\lib\router\layer.js:95:5)
<br> at next (C:\Users\Pablo\Node\node_modules\express\lib\router\route.js:137:13)
<br> at Route.dispatch (C:\Users\Pablo\Node\node_modules\express\lib\router\route.js:112:3)
<br> at Layer.handle [as handle_request] (C:\Users\Pablo\Node\node_modules\express\lib\router\layer.js:95:5)
<br> at C:\Users\Pablo\Node\node_modules\express\lib\router\index.js:281:22
<br> at Function.process_params (C:\Users\Pablo\Node\node_modules\express\lib\router\index.js:335:12)
<br> at next (C:\Users\Pablo\Node\node_modules\express\lib\router\index.js:275:10)
<br> at C:\Users\Pablo\Node\node_modules\body-parser\lib\read.js:130:5
</pre>
</body>
</html>
知道错误的来源吗?可能
req.body.payload
是不是有效的JSON字符串的对象,您可以使用JSON.stringify
而不使用JSON.parse
可能req.body.payload
是不是有效JSON字符串的对象,您可以使用JSON.stringify
而不使用JSON.parse
情况似乎就是这样。一旦移除,它工作得很好。谢谢,似乎是这样的。一旦移除,它工作得很好。谢谢
var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // Body parser use JSON data
app.post('/launches', function(req, res) {
var outputFilename = './spaceReport.json'; // path of the file to output
fs.writeFileSync(outputFilename, JSON.stringify(JSON.parse(req.body.payload), null, 4)); // write to the file system
res.send('Saved to ' + outputFilename);
});
var port = 3000;
app.listen(port);
console.log('Express started on port %d ...', port);
fs.writeFileSync(outputFilename, JSON.stringify(req.body.payload), null, 4);