为什么收到的数据与Electron事件发送的不同?

时间:2015-08-06 00:10:14

标签: javascript node.js mongodb mongoose electron

我正在尝试在Electron中进行CRUD操作,其中使用MongoDBMongoose Electron连接到数据库。

我有一个函数list用于返回Mongoose的“返回”。当我调试返回数据时,在我的终端中显示正确

  ipc.on('products.list', function(event, query) {
    Service.list(query, function(err, data) {
      console.log(data);  // this will be print correct result
      event.sender.send('product.list', data);
    });
  });

代码上半部分的结果:

[ { _id: 55c15f2981260a6f0ef8a657,
    __v: 0,
    deleted_at: '',
    updated_at: '',
    created_at: Tue Aug 04 2015 21:51:52 GMT-0300 (BRT),
    measure: '',
    aplication: '',
    model: 'Mode XBA',
    reference: 'Reference test 123',
    code: '2028' } ]

但是当我通过客户端调试返回数据时,结果不正确并且不等于服务器端打印。

客户端代码:

ipc.on('product.list', function(data) {
    window.data = data;
    console.log(data); // this will print incorrect result
});
ipc.send('products.list', {});

收到客户方的结果:

{  
   "$__":{....},
   "_doc":{...},
   "_posts":{...},
   "_pres":{...},
   "isNew":false
}

在每个元素中包含更多对象。

为什么会这样?如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题!

此返回来自Mongoose

{  
   "$__":{....},
   "_doc":{...},
   "_posts":{...},
   "_pres":{...},
   "isNew":false
}

对于仅返回属性,需要添加选项{lean : true}

例如:

Model.find(this.query)
  .lean()
  .exec(function(err, data) {}
);

Mongoose Doc

中的更多细节
相关问题