我无法在保存用户数据之前更改用户的roleid我想将其角色ID更改为2并保存

时间:2018-05-30 10:51:46

标签: node.js mongodb mongoose

我正在使用他的公司名称保存新用户,如果该公司名称是新的,那么我必须将他的roleid设置为2(经理),否则默认为1。

这是我的代码

Users.findOne({companyName:req.body.companyName},function(err,companyFound){
    if(err){
      console.log('error while fnding company name:'+err)
      return res.json({result:false,msg:err});
    }
    else if(!companyFound){
      console.log('company name not found :'+req.body.companyName);
       Users.findOne({username:req.body.username},function(err,userfound){
    if(err){
      console.log(err)
      return res.status(500).send('error while signup'+err)
    }
    if(!userfound){
      console.log("============================");
      console.log('ok... this is unique username : ' + req.body.username)
      user.roleId = 2;  /// setting roleid to 2/// problem here
      console.log('log before saving manager'+user);
      user.save(function(err,managersaved){
        if(err){
          console.log('error while saving signup manager data'+err);
          return res.json({result:false,msg:err});
        }
        else if(managersaved){
          console.log('guys we have new manager');
          return res.json({result:false,Data:managersaved});
        }
      })

    }

这是用户变量

    var user = new Users({
    username: req.body.username,
    name: req.body.name,
    mobile: req.body.mobile,
    email: req.body.email,
    password: req.body.password,
    managerId: req.body.managerId,
    roleId: req.body.roleId,
    companyName: req.body.companyName
  });

0 个答案:

没有答案