简体   繁体   中英

SyntaxError: UNexpected token n

I'm learning how to use the MEAN stack and to practice I'm doing a web that ask you for your name, your email and a course you've done recently. Then it stores the information to a DB. I can't find the error and maybe is an easy one.

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var port = process.env.PORT || 8080;
var Schema = mongoose.Schema;
var User = require('./user');

app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());
mongoose.connect('mongodb://localhost');

app.use(morgan('dev'));

var apiRouter = express.Router();
apiRouter.route('/')
.post(function(req, res) {
    var user = new User();
    user.name = req.body.name;
    user.course = req.body.course;
    user.mail = req.res.mail;
    user.save(function(err) {
        console.log(user.name);
        res.json({ message: 'Thank you!'});
    });
}).get(function(req, res) {
    User.find(function(err, users) {
        if (err) res.send(err);
        res.json(users);
    });
    res.json({ message: 'YEAAAAHHHH!'});
});

app.use('/', apiRouter);

app.listen(port);
console.log('Magic happens on port' + port);

And this is the user.js:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var UserSchema = new Schema({
    name: {type: String, required: true},
    course: {type: String, required: true},
    mail: {type: String, required: true}
});

module.exports = mongoose.model('User', UserSchema);

Thank you! :D

EDIT: sorry I forgot to put the error :

SyntaxError: Unexpected token n
at parse (/Users/pingu/Documents/mean_project/node_modules/body-parser /lib/types/json.js:83:15)
at /Users/pingu/Documents/mean_project/node_modules/body-parser/lib/read.js:116:18
at invokeCallback (/Users/pingu/Documents/mean_project/node_modules/raw-body/index.js:262:16)
at done (/Users/pingu/Documents/mean_project/node_modules/raw-body/index.js:251:7)
at IncomingMessage.onEnd (/Users/pingu/Documents/mean_project/node_modules/raw-body/index.js:308:7)
at emitNone (events.js:67:13)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at nextTickCallbackWith2Args (node.js:474:9)
at process._tickCallback (node.js:388:17)

Unexpected token is an error message produced by JSON.parse , so you are

  1. telling your server to expect JSON, and
  2. not supplying valid JSON.

This is because you supply a Content-type: application/json header in your request, but you're supplying form-type urlencoded data in your body like name=foobar&course=baz&...

Simply remove the JSON Content-type so your server will parse the body correctly as form data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM