我正在尝试以下代码:

app.get('/caminhosRelevantes',function(req,res){

console.log(req.protocol+":/"+req.get('host'));
   var collection = "caminhos";
   var ObjectId = require('mongodb').ObjectID;
   collectionDriver.getCollection(collection, function(error, the_collection) {
        if (error) {
res.end("<h1>Eerro</h1>");
        }
        else
          the_collection.aggregate([
  { $match: { id_usuario: "54bef57a14b88ad70a8ab74e" } },
  { $group: { _id: { rua: "$rua", data: "$data" } } },
  { $group: { _id: "$_id.rua", data: { $push: "$_id.data" }, count: { $sum: 1 } } },
  { $match: { count: { $gt: 5 } } }
], function(error,doc) { //C
              if (error) res.end(error);
              else res.end(doc);
            });
   });
});


但出现以下错误:

/server/node_modules/mongodb/lib/mongodb/connection/base.js:242
        throw message;
              ^
TypeError: first argument must be a string or Buffer
    at ServerResponse.OutgoingMessage.write (http.js:851:11)
    at ServerResponse.OutgoingMessage.end (http.js:985:16)

最佳答案

我尝试使用res.status(200).send(doc)而不是res.end(doc);,效果很好!

09-16 16:53