这是我的登录路由代码,我尝试调用 docs._id
但它返回了 null
。当我只调用 docs
时,它会在服务器端显示:
[ { _id: EX 唯一 ID,
用户名:{ 用户:'EXUsername',搜索:'exusername' },
通过:'EXpassword',
电子邮件:'EXemail'}]
我真的很困惑为什么我根本无法调用 docs.pass
或 docs._id
但 docs
本身有东西,我可以调用 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/