node.js - Mongoose 引用 _id 不起作用

标签 node.js mongoose

这是我的登录路由代码,我尝试调用 docs._id 但它返回了 null。当我只调用 docs 时,它会在服务器端显示:

[ { _id: EX 唯一 ID, 用户名:{ 用户:'EXUsername',搜索:'exusername' }, 通过:'EXpassword', 电子邮件:'EXemail'}]

我真的很困惑为什么我根本无法调用 docs.passdocs._iddocs 本身有东西,我可以调用 docs 并且它会返回数据。

我在node.js中的代码:

router.post('/loginUser', function(req, res){
  CheckingLogin(req.db, req.body.username, req.body.password, function(bool, docs){
    if(bool && exists(docs)){
    var err = false;
    var handleError = function(e){
    if(err){
      err = true;
      var eDoc = {};
      eDoc.errorCode = 1;
      eDoc.error = e;
      console.log(eDoc);
    }
  }
  /////problem here//////////////////////////////////
  var userID = docs._id;
  console.log(docs._id);
  if(userID != null){
    if(!err){
      tokens.db = req.db;
      tokens.refresher.create(userID, function(token){
        console.log('in token refresher ' + docs);
        res.send('1');
      });
    }else{
      console.log('Token err = true');
    }
  }else{
    console.log('Creator token = null');
    res.send('0');
  }
    }else{
        res.send('0');
    }
  });
});

最佳答案

嘿,它是一个对象数组,所以只需像这样 docs[0]._id 那样获取它,它就会为您工作。

关于node.js - Mongoose 引用 _id 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41557532/

相关文章:

javascript - 无法在 gatsby 构建中使用文档,必须转换为 Hook

node.js - 比较 MongoDB 中的两个日期字段

node.js - Mongoose bulkWrite - 'q' 的错误类型

mongodb - Mongoose 模式中的嵌套 map

javascript - Mongoose 模式中的加入/查找聚合

javascript - 在 MongoDB 查询中使用 javascript 变量作为 $regex 的值

javascript - 引用错误 : exit is not defined at repl:1:1 (in node. js)

javascript - Reactjs Nodejs通过axios上传ftp文件

node.js - 使用coffeescript的 Node 中的全局变量

javascript - 如何处理 MongoDB/mongoose 中的转换错误