0

经过 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 对象。

我怎样才能进入这些领域?

4

1 回答 1

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
});
于 2015-02-26T22:23:23.577 回答