【问题标题】:Do something if nothing found with .find() mongoose如果 .find() mongoose 没有找到任何东西,请执行某些操作
【发布时间】:2012-03-28 11:16:42
【问题描述】:

我将一些数据存储在 mongodb 中,并使用 js/nodejs 和 mongoose 访问它。我可以使用 .find() 在数据库中找到一些东西就好了,这不是问题。问题是如果没有什么,我想做点别的。目前这是我正在尝试的:

UserModel.find({ nick: act.params }, function (err, users) {
  if (err) { console.log(err) };
  users.forEach(function (user) {
    if (user.nick === null) {
      console.log('null');
    } else if (user.nick === undefined) {
      console.log('undefined');
    } else if (user.nick === '') {
      console.log('empty');
    } else {
      console.log(user.nick);
    }
  });
});

当我执行 act.params 不在 nick 索引中的操作时,这些都不会触发。发生这种情况时,我根本没有得到任何安慰,但是当它实际存在时,我确实让 user.nick 记录得很好。我只是试着像这样反过来做:

UserModel.find({ nick: act.params }, function (err, users) {
  if (err) { console.log('noooope') };
  users.forEach(function (user) {
    if (user.nick !== '') {
      console.log('null');
    } else {
      console.log('nope');
    }
  });
});

但这仍然没有记录nope。我在这里错过了什么?

如果它没有找到它,它只会跳过 find 调用中的所有内容,这很好,但如果它不存在,我需要在之后做一些我不想做的事情。 :/

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    当没有匹配时 find() 返回[],而 findOne() 返回null。所以要么使用:

    Model.find( {...}, function (err, results) {
        if (err) { ... }
        if (!results.length) {
            // do stuff here
        }
    }
    

    或:

    Model.findOne( {...}, function (err, result) {
        if (err) { ... }
        if (!result) {
            // do stuff here
        }
    }
    

    【讨论】:

    • 将此作为解释 find()findOne() 之间返回的差异并提供答案的答案!
    • // do stuff here 是否代表何时找到该项目?
    【解决方案2】:
    UserModel.find({ nick: act.params }, function (err, users) {
      if (err) { console.log(err) };
      if (!users.length) { //do stuff here };
      else {
        users.forEach(function (user) {
          console.log(user.nick);
        });
      }
    });
    

    这是我发现的工作。

    【讨论】:

    • 自 2012 年以来,事情一定发生了变化。我今晚回家后会再试一次,如果这不起作用而 users.length 起作用,我将编辑答案以包括它。
    【解决方案3】:

    我不得不使用:

     if(!users.length) { //etc }
    

    让它工作。

    【讨论】:

      【解决方案4】:

      使用这个。

      if(!users) { 
          res.send({message: "User not found"});
      }else {
          res.send({message: "User found"});
      }
      

      【讨论】:

        【解决方案5】:

        在您使用res.send(data)console.log(data) 后,您的代码确实返回[]empty log/ empty res.send 试试这个代码:

        if (data === null) {
                console.log('record does not exist')
                // or do something
            }
        

        我的项目中的示例

        router.post('/updated-data', (req, res, next) => {
        
        const userName = req.body.userName;
        const newUserName = req.body.newUserName;
        
        
        User.findOneAndUpdate({userName: userName}, {userName: newUserName}, {new: true}, (err, data) => {
        
            if (err) {
                console.log(err)
            } else if (data === null || !data) {
                res.send('kullanıcı bulunamadı')
            } else {
                console.log(newUserName)
                res.send(data)
            }
        })
        

        注意:其他示例可能更好用。

        【讨论】:

          猜你喜欢
          • 2016-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多