检索子数组(MEAN堆栈)

时间:2015-06-17 20:43:45

标签: angularjs node.js mongodb mongoose mean-stack

我有一个非常具体的问题。

所以我使用以下方法查询我的数据库(在NodeJS中使用express进行Mongoose调用):

    UserProfileModel.find({'username': req.query.username},'progressmanual' ,function (err, entries) {
            console.log("entries from the server:"+entries);
            res.json(entries);

        });
  

此查询产生以下结果:

    { _id: 5580edc16f5d5c3411f703c8,
progressmanual:[ { Date: Tue Jun 16 2015 23:47:36 GMT-0400 (Eastern Daylight Time), cups: 5, miles: 5, steps: 6, duration: 6, _id: 5580edd86f5d5c3411f703c9 },
{ Date: Tue Jun 16 2015 23:47:41 GMT-0400 (Eastern Daylight Time), cups: 754, miles: 57457, steps: 457547, duration: 54745745, _id: 5580eddd6f5d5c3411f703ca },
{ Date: Tue Jun 16 2015 23:47:45 GMT-0400 (Eastern Daylight Time), cups: 547457, miles: 536346, steps: 4436346, duration: 466, _id: 5580ede16f5d5c3411f703cb },
{ Date: Tue Jun 16 2015 23:47:50 GMT-0400 (Eastern Daylight Time), cups: 436, miles: 6436, steps: 4464346, duration: 446, _id: 5580ede66f5d5c3411f703cc },
{ Date: Tue Jun 16 2015 23:49:36 GMT-0400 (Eastern Daylight Time), cups: 5, miles: 5, steps: 6, duration: 6, _id: 5580ee506f5d5c3411f703cd } ], personalgoals: {} }

在我的前端,我正在尝试显示progressmanual中的条目 所以当我尝试res.json(entries.progressmanual)时,它会导致null。你知道如何提取progressmanual个对象的数组吗?

另外,你知道为什么查询结果包含我没有请求的额外“列”(除了progressmanual之外的东西)吗?

1 个答案:

答案 0 :(得分:1)

感谢评论 Kevin B ,我发现了问题:将find()更改为findOne,让我获得了entries.progressmanual。所以看起来find()始终返回数组,所以使用find()我应该使用entries[0].progressmanual