SyntaxError:未预期的令牌n

时间:2016-02-23 13:20:36

标签: node.js mean-stack

我正在学习如何使用MEAN堆栈并练习我在网上询问您的姓名,电子邮件以及您最近完成的课程。然后它将信息存储到DB。我无法找到错误,也许是一个简单的错误。

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);

这是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);

谢谢! :d

编辑:抱歉,我忘记了错误:

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)

1 个答案:

答案 0 :(得分:14)

Dim acSh As Worksheet Dim oLo As ListObject Sub StelDatabereikIn() Set acSh = ActiveSheet For Each oLo In acSh.ListObjects Application.Goto oLo.Range MsgBox "Databodyrange is " & oLo.DataBodyRange.Address oLo.DataBodyRange.Select 是由Unexpected token生成的错误消息,因此您

  1. 告诉您的服务器期望JSON和
  2. 不提供有效的JSON。
  3. 这是因为您在请求中提供了JSON.parse标题,但您在身体中提供了表单类型的urlencoded数据,例如Content-type: application/json

    只需删除JSON name=foobar&course=baz&...,这样您的服务器就会正确解析正文作为表单数据。