NodeJs正在发送空响应

问题描述 投票:0回答:3

我正在使用NodeJS和MongoDb作为后端服务。我试图根据条件检索不同的数据,例如我想获取订单状态为待定的名称:

const coll = client.db("My_db").collection("Orders");
            coll.distinct("name",{order_status:"pending"},(function(err,docs){

                  let output = docs.map(r => ({'name':r.name}));
                  res.send(output);       

            }));

但是它正在发送像这样的空数组:

[
 {},
 {},
 {},
 {},
 {},
 {}
]

有人请让我知道我在做什么错。任何帮助将不胜感激。

谢谢

javascript node.js mongodb
3个回答
0
投票

根据文档Distinct Mongo DB

Thay结果将是数组。但是您要匹配r.name其对象键。

仅对参数也是如此

let output = docs.map(r => ({'name':r}));

0
投票

您无需针对该响应运行地图。与众不同的函数返回不同值的数组。尝试console.log(docs)并检查它


0
投票

因为您已将字段指定为名称,所以不需要使用r.name,只需使用r

您可以参考link,希望这会对您有所帮助:)

如果您的MongoDB高于2.2,并且您想查找多个字段,则可以使用聚合

collection = db.tb;
collection.aggregate([
  {"$group": { "_id": { name: "$name", email: "$email" } } }
]);
© www.soinside.com 2019 - 2024. All rights reserved.