尝试在MongoDB中保存文档时出错

时间:2017-02-07 18:22:47

标签: node.js mongodb

我有这种注册方法来保存用户。

exports.signup = function(req, res) {
// Initialize the variables
var user = new User(req.body);
var message = null;
user.provider = 'local';

// save the user
user.save(function(err) {
    if (err) {
        console.log(err);
        return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
        });
    } else {
        req.login(user, function(err) {
            if (err) {
                res.status(400).send(err);
            } else {
                res.json(user);
            }
        });
    }
  });
};

她是我的架构。

var UserSchema = new Schema({
name: {
    type: String,
    required: true
},
email: {
    type: String,
    index: true,
    match: [/.+\@.+\..+/, "Please fill valid e-mail address"]
},
username: {
    type: String,
    trim: true,
    unique: "Username should be unique",
    required: true
},
password: {
    type: String,
    validate: [
        function (password) {
            return password && password.length > 6;
        },
        "Password should be greater than six letters"
    ]
},
salt: {
    type: String
},
provider: {
    type: String,
    required: "Provider is required"
},
providerId: String,
providerData: {},
created: {
    type: Date,
    default: Date.now()
}
});

当我在空集合中发布帖子请求时,第一个被保存,但在此之后我收到此错误。

MongoError: Projection cannot have a mix of inclusion and exclusion.
at Function.MongoError.create (/home/sinnedde/WebstormProjects/mean-chatapp/node_modules/mongodb-core/lib/error.js:31:11)
at queryCallback (/home/sinnedde/WebstormProjects/mean-chatapp/node_modules/mongodb-core/lib/cursor.js:212:36)
at /home/sinnedde/WebstormProjects/mean-chatapp/node_modules/mongodb-core/lib/connection/pool.js:455:18
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
POST /signup 500 1445.231 ms - 615

请帮忙。

首先是req.body

{
"name":"John",
"email": "johndoe@gmail.com",
"username": "john123",
"password": "password"
}

第二个req.body

{
"name":"Jane",
"email": "janedoe@gmail.com",
"username": "jane123",
"password": "password"
}

0 个答案:

没有答案