错误:未处理的“错误”事件。 (E11000重复键错误索引:eventica.users。$ walletAddress_1 dup键:{:null))

时间:2019-07-04 13:23:07

标签: express mongoose

我正在为管理员和用户使用相同的集合,但是管理员使用钱包地址字段,并且它是唯一的,但是用户不需要钱包地址,因此当用户签名过程中出现错误时,我没有架构。我的代码有什么问题?

在管理员注册时,相同的数据库和相同的集合,集合模式更改

错误:

  

错误:未处理的“错误”事件。 (E11000重复键错误索引:eventica.users。$ walletAddress_1 dup键:{:null))

模式:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcryptjs');

const UserSchema = new Schema({
    firstname: String,
    lastname: String,
    gender: String,
    dob: {
        type: Date
    },
    email: String,
    password: String,
    mobileNo: String,
    nationality: String,
    country: String,
    residence: String,
    address: String,
    employmentStatus: String,
    jobTitle: String,
    currentEmployer: String,
    industry: String,
    annualIncome: String,
    otherIncome: String,
    religion: String,
    netAsset: String,
    declaration: {
        type: Boolean,
        default: false
    },

    status: {
        type: String,
        default: 'New'
    },
    created: {
        type: Date,
        default: Date.now
    },
    seed: String,
    restToken: String,
    accountType: {
        type: String,
        default: 'User'
    },
    permenantAddress: {
        type: Object
    },
    presentAddress: {
        type: Object
    },
    uid: {
        type: String
    },
    proimg: String
});

UserSchema.methods.comparePassword = function comparePassword(password, callback) {
    bcrypt.compare(password, this.password, callback);
};

控制器Javascript文件:

app.post("/signup", function (req, res) {

    sess = req.session;

    var email = req.body.email;
    var password = req.body.password;
    var repeatpassword = req.body.repeatpassword;
    var firstname = req.body.firstname;
    var lastname = req.body.lastname;

    const userData = req.body;

    // var currency = "USD";
    //  if (country == "Korea, Republic of") {
    //       currency = "KRW"
    //  } 

       // userData["status"] = config.get('general.userSignupInitStatus'); // "PENDING";
        // userData["currency"] = currency;

    const newUser = new User(userData);

    if (password != repeatpassword) {
      //  req.flash('error', 'Password and Confirm password are not same....');
      return res.redirect('/signup');
    }


    User.findOne({ email: req.body.email }, (errUser, userFound) => {
      if (errUser) {
        req.flash('error', 'Sign up...error, please try again...');
        return res.redirect('/signup');
      }
      if (userFound) {
        req.flash('error', 'User account exists for this email address, reset password if you forgot the password');
        return res.redirect('/signup');
      }
      else {
        newUser.save((err) => {
          if (err) { console.log(err); throw err.message; }
          // mail chimp integration 

          sess.user = newUser;
          res.redirect("/eventica");
        }); // save
      } // if user found
    }); //findUser
  }); // signup

0 个答案:

没有答案