2015-04-04 55 views
1

我正在经历问题,它要求我计算每个班级中孩子的最低分数值。我写了下面的源代码。
我使用蒙戈2.6.9和节点v0.10.25和Ubuntu 14.04 LTSTypeError:对象不是使用MongoDB和Node.js的函数

var MongoClient=require('mongodb').MongoClient; 
var server=require('mongodb').Server; 

var mongoclient=new MongoClient(new server("localhost",27017)); 

mongoclient.connect("mongodb://localhost:27017/",function(err,db){ 
if(err) throw err; 

var db=mongoclient.db('school'); 

cursor=db.collection('students').aggregate(
[ 
    {$match : {"scores.type" : "homework"}}, 
    {$unwind:"$scores"}, 
    {$group : {_id : '$name', 
    'minimum' : { $min :"$scores.score" } 
} 
} 
]); 
}); 

在使用节点app.js运行此汇总查询给出了这样的错误

/home/oroborus/node_modules/mongodb/lib/mongodb/connection/base.js:246 
     throw message;  
      ^
TypeError: object is not a function 
    at /home/oroborus/node_modules/mongodb/lib/mongodb/collection/aggregation.js:317:7 
    at /home/oroborus/node_modules/mongodb/lib/mongodb/db.js:1195:7 
    at /home/oroborus/node_modules/mongodb/lib/mongodb/db.js:1903:9 
    at Server.Base._callHandler (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/base.js:453:41) 
    at /home/oroborus/node_modules/mongodb/lib/mongodb/connection/server.js:488:18 
    at MongoReply.parseBody (/home/oroborus/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5) 
    at null.<anonymous> (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/server.js:446:20) 
    at EventEmitter.emit (events.js:95:17) 
    at null.<anonymous> (/home/oroborus/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13) 
    at EventEmitter.emit (events.js:98:17) 

它根据我是来自聚合函数。但是当我在mongo终端运行相同的查询时,我得到了正确的输出结果。

cursor=db.students.aggregate([ {$match : {"scores.type" : "homework"}}, {$unwind:"$scores"}, {$group : {_id : '$name', 'minimum' : { $min :"$scores.score" } } } ]); 
{ "_id" : "Myrtle Wolfinger", "minimum" : 35.99397009906073 } 
{ "_id" : "Gennie Ratner", "minimum" : 34.50565589246531 } 
{ "_id" : "Nobuko Linzey", "minimum" : 19.27081566886746 } 
{ "_id" : "Flora Duell", "minimum" : 40.68238966626067 } 
{ "_id" : "Shin Allbright", "minimum" : 52.68629677727286 } 

Ques-什么,在哪里是错误,如何纠正它。
谢谢。

回答

5

collection.aggregate()的最后一个参数需要是回调。 mongodb驱动程序期待函数,但最后一个参数是一个对象。这就是你得到这个错误的原因。这里是回调的修改代码:

var MongoClient = require('mongodb').MongoClient; 
var server = require('mongodb').Server; 

var mongoclient = new MongoClient(new server("localhost", 27017)); 

mongoclient.connect("mongodb://localhost:27017/", function(err, db) { 
    if (err) throw err; 

    var db = mongoclient.db('school'); 

    cursor = db.collection('students').aggregate(
    [ 
     {$match: {"scores.type": "homework"}}, 
     {$unwind: "$scores"}, 
     { 
      $group: { 
       _id: '$name', 
       'minimum': {$min: "$scores.score"} 
      } 
     } 
    ], function(err, result) { // callback 
     console.dir(result); 
     db.close(); 
    } 
); 
}); 
+0

这就像一个魅力。我徘徊了10个帖子,如果我不会在这里发布它,我将永远无法得到答案。万分感谢。 :) – 2015-04-04 22:43:33

+0

你能回答[这](http://stackoverflow.com/questions/29452500/typeerror-cannot-call-method-each-of-undefined-node-js) – 2015-04-04 23:31:40

相关问题