使用Node.js从邮递员发送数据时出错

发送postman的请求时出现以下错误,我使用Node.js作为后端。

错误:

 POST /api/users/save-card-details 400 31.719 ms - 871 SyntaxError: Unexpected token - at parse (/opt/lampp/htdocs/heroku/FGDP/node_modules/body-parser/lib/types/json.js:83:15) at /opt/lampp/htdocs/heroku/FGDP/node_modules/body-parser/lib/read.js:116:18 at invokeCallback (/opt/lampp/htdocs/heroku/FGDP/node_modules/raw-body/index.js:262:16) at done (/opt/lampp/htdocs/heroku/FGDP/node_modules/raw-body/index.js:251:7) at IncomingMessage.onEnd (/opt/lampp/htdocs/heroku/FGDP/node_modules/raw-body/index.js:307:7) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9) 

我在下面解释我的代码。

server.js:

  var express=require('express'); var morgan = require('morgan'); var http=require('http'); var bodyParser= require('body-parser'); var methodOverride = require('method-override'); var mongo = require('mongojs'); var session = require('express-session'); var app=module.exports=express(); var server=http.Server(app); var port=8989; var admin=require('./route/route.js'); var api=require('./api/api.js'); app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users app.use(morgan('dev')); // log every request to the console app.use(bodyParser.urlencoded({ extended: false,limit: '5mb' })) // parse application/x-www-form-urlencoded app.use(bodyParser.json({limit: '5mb'})) // parse application/json app.use(methodOverride()); // simulate DELETE and PUT app.use(session({secret: 'FGDPlexel',resave: true,saveUninitialized: true})); app.get('/',function(req,res){ res.sendFile(__dirname + '/index.html'); }) app.post('/api/users/save-card-details',api.saveCardDetails); 

api.js:

 var multer = require('multer'); var storage =multer.diskStorage({ destination: function (req, file, callback) { callback(null, './../uploads'); }, filename: function (req, file, callback) { callback(null, Date.now()+'-'+file.originalname); } }); var upload = multer({ storage: storage }); exports.saveCardDetails=function(upload.single('image'),req,res){ var name=req.body.name; var company=req.body.company; var position=req.body.position; var mobile=req.body.mobile; var email=req.body.email; var landline=req.body.landline; var url=req.body.url; var postcode=req.body.postcode; var address=req.body.address; var image=req.body.image; var userid=req.body.userid; var profiletext=req.body.profile; var biography=req.body.biography; var token_id=req.body.token_id; console.log('request',req); } 

在这里,我使用postman发送请求,并设置标题Content-Type:application/json 。 屏幕截图如下。

在这里输入图像描述

在这里,我需要上传图像,然后获取图像名称以及所有其他数据,但得到上述错误。 请帮帮我。

我不知道为什么,但看起来像请求不包含有效的JSON。

如果你会去的

 /opt/lampp/htdocs/heroku/FGDP/node_modules/body-parser/lib/types/json.js:83:15) 

你会看到这个function

 function parse(body) { if (body.length === 0) { return {} } if (strict) { var first = firstchar(body) if (first !== '{' && first !== '[') { debug('strict violation') throw new SyntaxError('Unexpected token ' + first) <--- Your error } } debug('parse json') return JSON.parse(body, reviver) } 

node.js的好处是您可以轻松更改库的代码。 所以我会做一个console.logbody ,看看实际发生了什么。