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

时间:2017-08-19 09:48:18

标签: node.js postman multer

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。屏幕截图如下所示。

enter image description here

在这里,我需要先上传图像,然后获取图像名称以及所有其他数据,但会出现上述错误。

1 个答案:

答案 0 :(得分:0)

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

如果你要去

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

您将看到此功能

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.log body drop table if exists my_table; create table my_table(id int primary key, a int, b text, c date); insert into my_table values (1, 1, 'old text', '2017-01-01'); 看看实际发生了什么。