经过10个小时的尝试后,我无法理解这一点:
如果我
users.findById(req.body.user_id,function(e,doc){});
和console.log返回的文档,看起来都很好:
{ _id: 54dcad6de4b01007caacb0cd,
username: 'realizertest',
password: '******************************',
first_name: 'Realizer',
second_name: 'Test',
display_name: 'Realizer Test',
email: 'system@realizerlabs.com' }
但是,在尝试访问包含的字段时,例如由:
user = users.findById(req.body.user_id,function(e,doc){});
var user_email = user.email;
我只是未定义。用户对象如下所示:
{ col:
{ manager:
{ driver: [Object],
helper: [Object],
collections: [Object],
options: [Object],
_events: {} },
driver:
{ _construct_args: [],
_native: [Object],
_emitter: [Object],
_state: 2,
_connect_args: [Object] },
helper: { toObjectID: [Function], id: [Ob
name: 'users',
col:
{ _construct_args: [],
_native: [Object],
_emitter: [Object],
_state: 2,
_skin_db: [Object],
_collection_args: [Object],
id: [Object],
emitter: [Object] },
options: {} },
type: 'findOne',
opts: { fields: {}, safe: true },
domain: null,
_events:
{ error: [ [Function], [Function] ],
success: [ [Function], [Function] ] },
_maxListeners: 10,
emitted: {},
ended: false,
success: [Function],
error: [Function],
complete: [Function],
resolve: [Function],
fulfill: [Function],
reject: [Function],
query: { _id: 54dcad6de4b01007caacb0cd } }
我也试过user.query.email
,但得到的结果相同。
findById显然没有返回我可以这种方式使用的JSON对象。
我怎样才能进入这些领域?
答案 0 :(得分:1)
这是一个异步调用,所以你需要使用回调,你不能将该函数分配给变量:
users.findById(req.body.user_id,function(e,doc){
var user = doc;
console.log(user); //should see the object now, and access the props
});