我有一个连接到Mongoose db的Express服务器(我的术语可能不正确,因为我刚开始学习Express)。当我访问/ admins(route?)时,我收到一个内部带有JSON对象的对象。
我发错了吗?
var userSchema = new Schema({
firstName: {type: String, trim: true},
lastName: {type: String, trim: true},
classYear: Number,
email: {type: String, unique: true, sparse: true, trim: true},
phone: {type: String, unique: true, sparse: true},
phoneProvider: {type: String, trim: true},
isAdmin: {type: Boolean, index: true},
isSuperAdmin: {type: Boolean, index: true},
hash: String,
companyName: {type: String, trim: true},
interests: [String],
},
{
toObject: { getters: true },
timeStamps: {
createdAt: 'createdDate',
updatedAt: 'updatedDate'
}
}
);
admins.js的一部分
exports.createAdmin = function(req, res, next) {
if (typeof req.body.email !== 'string')
return res.status(400).send('Missing email');
if (typeof req.body.password !== 'string' && typeof req.body.hash !== 'string')
return res.status(400).send('Missing password');
if (typeof req.body.companyName !== 'string')
return res.status(400).send('Missing companyName');
req.body.email返回undefined。收到的JSON就像:
{ '{\n\temail:'an@email.com'}': ''}
这是我到/ admins的路线:
app.post('/admins', admins.createAdmin);
这是我的中间件:
app.param('id', function(req, res, next, id) {
if (!id.match(/^[0-9a-fA-F]{24}$/))
return res.status(400).send('Invalid ID');
next();
});