NodeJS express:当城镇已经存在时更新中的逻辑

时间:2018-03-17 11:55:19

标签: node.js express mongoose

我正在使用具有快速框架和mongoose的NodeJS,我使用逻辑进行城镇输入,其中没有输入,右输入和已经存在的输入。也许有更好的解决方案,但这很好用:

router.post('/create', (req, res)=>{
    let errors = [];

    if (!req.body.name) {
      errors.push({message: 'Add town name!'});
    }

    if (errors.length > 0) {
      res.render('admin/towns/index', {
        errors: errors,
        name: req.body.name
      });
    } else {
      Town.findOne({name: req.body.name}).then(town=>{
        if (!town) {
          const newTown = Town({
            name: req.body.name
          });
          newTown.save().then(savedTown=>{
            req.flash('success_message', `Town ${savedTown.name} is activated`);
            res.redirect('/admin/towns');
          });
        } else {
          req.flash('error_message', 'Town name already exist!');
          res.redirect('/admin/towns');
        }
      });
    }
});

但是,当我想要更新时,哪里没有输入工作正常,其中名称是新的也工作正常但如果名称存在它会显示我错误:

  

(node:12140)UnhandledPromiseRejectionWarning:MongoError:E11000   重复键错误集合:crm_electors.towns index:name_1 dup   key:{:“贝尔格莱德”}

router.put('/edit/:id', (req, res)=>{
    let errors = [];

    if (!req.body.name) {
      errors.push({message: 'Add town name!'});
    }

    if (errors.length > 0) {
      res.render('admin/towns/index', {
        errors: errors,
        name: req.body.name
      });
    } else {
      Town.findOne({_id: req.params.id}).then(town=>{
        if (req.body.name !== town) { // I guess this logic is wrong
          town.name = req.body.name;
          town.save().then(savedTown=>{
            req.flash('success_message', 'Town name successfully renamed');
            res.redirect('/admin/towns');
          });
        } else {
          req.flash('error_message', 'Town name already exist!');
          res.redirect('/admin/towns');
        }
      });
    }
});

我认为这个逻辑是错误的(req.body.name!== town),所以我尝试了req.body.name!== town.name和一些更多的变化,但没有运气。

1 个答案:

答案 0 :(得分:0)

Mongoose在错误第一次回调时工作,其中回调的第一个参数为error,第二个参数为result。由于error回调中的第一个参数是您使用变量result引用的错误,因此您拥有的代码会误导findOne town Town.findOne({_id: req.params.id}, (err, town)=> { if (town) { //check is this new name already exist or not Town.find({name: req.body.name}, (err, townWithName) => { if(townWithName){ //if there is already a town with this name req.flash('error_message', 'Town name already exist!'); res.redirect('/admin/towns'); } else { town.name = req.body.name; town.save((err, savedTown) => { req.flash('success_message', 'Town name successfully renamed'); res.redirect('/admin/towns'); }); } }); } else { req.flash('error_message', 'Town with this id do not exist!!'); res.redirect('/admin/towns'); } }); 。因此,您需要将代码更改为:

town

因为,name在回调的第二个参数中它保存了猫鼬模型记录。因此,您可以从中访问err属性。此外,您还可以通过在回调中使用此代码块在回调中使用if (err) { res.send(//some message and code); } 参数来处理运行时错误:

SELECT 
    [conversationId],
    [conversationStart]
FROM 
    [Customer].[dbo].[vw_Conversations]
WHERE 
    conversationStart BETWEEN '2018-01-01 16:00:00.000' AND '2018-01-31 06:59:59.000'