mongoose引用_id不起作用
这是我的代码login路线,我想要打电话给docs._id
但它给我回null
。 当我打电话给docs
它显示在服务器端:
[ { _id: EX unique id, username: { user: 'EXUsername', search: 'exusername' }, pass: 'EXpassword', email: 'EXemail' } ]
我真的很困惑,为什么我根本不能调用docs.pass
或docs._id
,但是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'); } }); });
嘿它的一个对象的数组,所以只是得到它像这个文档[0] ._ ID它会为你工作。