【问题标题】:NodeJs Mongoose findById returns empty doc with status trueNodeJs Mongoose findById 返回空文档,状态为 true
【发布时间】:2019-07-14 23:57:21
【问题描述】:

我正在尝试使用 MEAN 堆栈实现 CRUD 操作。我在通过 ID 获取用户时遇到问题。它显示状态为真,但它返回一个空文档。

这是我的模型:

const userSchema = new mongoose.Schema({
    fullName: {
        type: String,
        required: 'Full name can\'t be empty '
    },
    userName: {
        type: String,
        required: 'user name can\'t be empty ',
        unique: true
    },
    email: {
        type: String,
        required: 'email can\'t be empty ',
        unique: true
    });

mongoose.model('User', userSchema);

在我的控制器中:

const mongoose = require('mongoose');
const passport = require('passport');
var ObjectId = require('mongoose').Types.ObjectId;
const User = mongoose.model('User');

module.exports.getuser = (req, res, next) => {
    if(!ObjectId.isValid(req.params.id))
        return res.status(400).send(`No record with given id : ${req.params.id}`);

    User.findById(req.params.id, (err, user) => {
        if(!err){ res.status(200).json({status: true, user}); }
        else{ console.log('Error in retriving User :' + JSON.stringify(err, undefined, 2)); }
    });    
}

这是路线:

router.get('/:id', jwtHelper.verifyJwtToken, ctrlUser.getuser);

在检查邮递员时,我得到了 status: true 但它返回一个空白文档。 我不知道发生了什么事,请帮助。 提前致谢!!

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    你使用了错误的参数

    req.params._id 替换为req.params.id 如下:

    User.findById( req.params.id, (err, user) => {
            if(!err){ res.status(200).json({status: true, user}); }
            else{ console.log('Error in retriving User :' + JSON.stringify(err, undefined, 2)); }
        });    
    

    【讨论】:

    • 我尝试了其他一些答案,所以我使用“_id”它不起作用
    【解决方案2】:

    试试这个。另一种可能的情况是 req.params String 格式的 _id 而不是 ObjectId。

    var id = mongoose.Types.ObjectId(req.params.id);
    User.findById(id, (err, user) => {
        if(!err) { 
            res.status(200).json({status: true, user}); 
        }else {
            console.log('Error in retriving User :' + JSON.stringify(err, undefined, 2));
        }
    });
    

    【讨论】:

      【解决方案3】:

      findById 的语法无效

      使用 findByID 的正确语法是

      Adventure.findById(id, function (err, adventure) {});
      

      你也可以用 findOne 替换 findById

      User.findOne({_id: req.params.id}, (err, user) => {
              if(!err){ res.status(200).json({status: true, user}); }
              else{ console.log('Error in retriving User :' + JSON.stringify(err, undefined, 2)); }
          }); 
      

      或者你也可以使用它

        User.findById(req.params.id, (err, user) => {
                  if(!err){ res.status(200).json({status: true, user}); }
                  else{ console.log('Error in retriving User :' + JSON.stringify(err, undefined, 2)); }
              }); 
      

      希望这对你有用@sun

      【讨论】:

      • 我尝试使用这两种方案,但它没有返回任何文档@Vivek Ghanchi
      • 我认为您有空数据进行查找,请检查
      • 我插入了几条记录。剩余的凝乳操作工作正常,例如删除和插入检索所有用户但通过“id”获取用户时无法正常工作。
      猜你喜欢
      • 2019-07-15
      • 2020-05-13
      • 2020-09-16
      • 1970-01-01
      • 2021-10-30
      • 2020-09-09
      • 2017-07-21
      • 2013-06-16
      • 2016-09-11
      相关资源
      最近更新 更多