请看一下下面的代码,有什么问题?


处理注册的路由


 app.post('/reg', function (req, res, next) {
    var user = new Blog(req.body.user).save(function (err) {
        if (err) return next(err);
        res.redirect('/login/' + user.email);
    });
});

mongooseschemas的代码


 var mongoose = require('mongoose');
var BlogSchema = new mongoose.Schema({
    username: String,
    emailaddress: String,
    password: String
});

module.exports = BlogSchema;

创建module


 var mongoose = require('mongoose');
var BlogSchema = require('../schemas/blog');
var Blog = mongoose.model('Blog', BlogSchema);


module.exports = Blog;

图片描述

error信息

node.js mongoose

请叫我OOXX 9 years, 2 months ago

与代码无关,看你的报错信息,应该是mongodb中的blog表里面设置了一个不可重复的emailadress索引,你注册时用了与表中相同的email就会报这个错.
两种修改建议:
1.将这个索引改为可重复.
2.在save入库中先比对email是否存在.

H光大小姐 answered 9 years, 2 months ago

Your Answer