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)
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
Check if your punctuation is incorrect For example, omitting necessary or adding unknown punctuation marks in your coding will prompt the error. So, to fix the unexpected token problem, go through your codes for punctuation errors. Punctuation marks like parenthesis and commas must be proper in your coding.
To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.
Unexpected token
is an error message produced by JSON.parse
, so you are
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With