获取对 monk findById 调用的响应中的所有字段。 nodejs/express/monk

Getting at all fields in the response to a monk findById call. nodejs/express/monk

经过 10 个小时的尝试,我还是无法解决这个问题:

如果我

users.findById(req.body.user_id,function(e,doc){});

和 console.log 文档 returned,一切看起来都不错:

{ _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 显然不是 return 我可以用这种方式使用的 JSON 对象。

如何获取这些字段?

这是一个异步调用,所以你需要使用回调,你不能将该函数分配给一个变量:

users.findById(req.body.user_id,function(e,doc){
    var user = doc;
    console.log(user); //should see the object now, and access the props
});