TypeError:对象不是将MongoDB与Node.js一起使用的函数 [英] TypeError: object is not a function in using MongoDB with Node.js

查看:56
本文介绍了TypeError:对象不是将MongoDB与Node.js一起使用的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,它要求我计算班上每个孩子的最低分数.我写了下面的源代码.
我正在使用Mongo 2.6.9和Node v0.10.25和ubuntu 14.04 LTS

I was going through question and it asked me calculate minimum value of score for each child in class. I wrote the following source code.
I am using Mongo 2.6.9 and Node v0.10.25 and ubuntu 14.04 LTS

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运行时,此聚合查询会出现此错误

This aggregate query when run using node app.js gives this error

/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终端中运行相同的查询时,我得到了正确的输出.

Which according to me is coming from the aggregate function. But when i run the same query in mongo terminal i get a proper output.

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 }

是-错误在哪里,在哪里以及如何纠正.
谢谢.

推荐答案

collection.aggregate()的最后一个参数必须是回调. mongodb驱动程序需要一个函数,但是最后一个参数是一个对象.这就是为什么您会收到该错误的原因.这是带有回调的修改后的代码:

The last argument of collection.aggregate() needs to be a callback. The mongodb driver is expecting a function but your last argument is an object. That's why you're getting that error. Here is the revised code with the callback:

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();
    }
  );
});

这篇关于TypeError:对象不是将MongoDB与Node.js一起使用的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆