Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何禁止添加mongodb中已经存在的具有相同objectId的较小数字?_Node.js_Express_Mongoose - Fatal编程技术网

Node.js 如何禁止添加mongodb中已经存在的具有相同objectId的较小数字?

Node.js 如何禁止添加mongodb中已经存在的具有相同objectId的较小数字?,node.js,express,mongoose,Node.js,Express,Mongoose,如果我有这样的收藏: _id: ObjectId("5ae4e704b7bccf133c24f0b8") electoralUnit:ObjectId("5ab906612f30fe23dc592591") turnoutByHour:"08:00" voterTurnout:11 safeVoter:1 date:2018-04-28T21:26:28.678Z __v:0 如何禁止下一次输入,即09::00 in field turnoutByHour中的输入,不能是VoterTrump

如果我有这样的收藏:

_id: ObjectId("5ae4e704b7bccf133c24f0b8")
electoralUnit:ObjectId("5ab906612f30fe23dc592591")
turnoutByHour:"08:00"
voterTurnout:11
safeVoter:1
date:2018-04-28T21:26:28.678Z
__v:0
如何禁止下一次输入,即09::00 in field turnoutByHour中的输入,不能是VoterTrump field小于11的数字,但仅当ElectorNunit是相同的id时

我用if语句想象了一些东西,比如:

if (req.body.voterTurnout < VoterTurnouts.findOne({voterTurnout : req.params.voterTurnout }))

所以我只需要添加这个…

这很简单?在xxx.findOne之后使用if statement?我不确定你的意思以及应该是什么样子?Oke xxx.findOne({…}).exec(函数(err,result){if(req.body.voter..但是,如何将turnoutByHour和ElectorUnnit包括在if语句中?我添加route让您看到我有很多带有错误消息的if语句,但我不知道如何添加此语句。
router.post('/', (req, res)=>{
    let errors = [];

    if (!req.body.electoralUnit) {
      errors.push({message: 'Must chooose Electoral Unit!'});
    }
    if (!req.body.voterTurnout) {
      errors.push({message: 'Must add Voter Turnout!'});
    }

    if (errors.length > 0) {
      res.render('home/voterTurnouts/index', {
        errors: errors,
        electoralUnit: req.body.electoralUnit,
        voterTurnout: req.body.voterTurnout
      });
    } else {
      VoterTurnout.findOne({_id: req.params.id}).then(voterTurnout=>{
          const newVoterTurnout = VoterTurnout({
            electoralUnit: req.body.electoralUnit,
            turnoutByHour: req.body.turnoutByHour,
            voterTurnout: req.body.voterTurnout,
            safeVoter: req.body.safeVoter
          });
          newVoterTurnout.save().then(savedVoterTurnout=>{
            ElectoralUnit.find({}).then(electoralUnits=>{
              res.redirect('/voterTurnouts');
            });
          }).catch(errors=>{
            req.flash('error_message', 'Hours are already added with that Electoral Unit!');
            res.redirect('/voterTurnouts');
          });
      });
    }
});