简体   繁体   中英

error while inserting data into database express/mongodb app

Aloha, i was trying to insert data from a form into my database unfortunately there's an error keep showing up and i don't understand why, the error is Error: Can't set headers after they are sent. and i keep getting something is wrong in code which i console.log it indicate something is wrong in my database

UPDATED

this is the error currently i get http://imgur.com/ROl06t7

{ [MongoError: E11000 duplicate key error index: symsal.users.$username_1  dup key: { : null }]
  name: 'MongoError',
  err: 'E11000 duplicate key error index: symsal.users.$username_1  dup key: { : null }',
  code: 11000,
  n: 0,
  connectionId: 84,
  ok: 1 }

my code

 this.handleSignup = function(req, res, next) {
        "use strict";
    validateSignup(req.body, function(error, data) {
        if(error) {
            res.send(400, error.message);
        } else {
            users.addUser(data, function(err, user) {
                "use strict";
                sessions.startSession(data, function(err, session_id) {
                    "use strict";
                    if (err) return next(err);
                    res.cookie('session', session_id);
                });
            });
            res.send(200);
        }
    })

    }

addUser

 this.addUser = function(data,callback) {
        "use strict";

        var salt = bcrypt.genSaltSync();
        var password_hash = bcrypt.hashSync(data.password, salt);
        var user = {
            '_id': data.email,
            'email':data.email,
            'password': password_hash,
            'firstName': data.firstName,
            'lastName': data.lastName,
            'penName': data.penName,
            'publicUsername':data.publicUsername
        };
        console.log(user);
        users.insert(user, function (err, result) {
            "use strict";

            if (!err) {
                console.log("Inserted new user");
                return callback(null, result[0]);
            }else{
                console.log("Something is wrong");
            }

            return callback(err, null);
        });
    }

if delete return callback(err, null); the Error: Can't set headers after they are sent. will not show up but it still display "Something is wrong" in my terminal. is there any mistake in my code ? i believe return callback(err, null); is to tell the application there's an error and return null to the application.

hope you guys can help me thank you

The error means that you have declared that the username property should be unique, yet you're trying to insert a new document with a username that already exists in the database.

Since you're not actually setting the username property on your document, MongoDB will insert a null value for each new document (and inserting a second null value for a unique property will also violate the uniqueness constraint).

If you're not using the username property anymore, you should remove the unique index on it. From the MongoDB shell, that would look something like this:

db.users.dropIndex({ username : 1 })

If you do want to keep using it, but don't want to have MongoDB insert a null if you don't declare the username property on a document, use the sparse option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM