简体   繁体   English

TypeError:无法调用未定义的node.js的方法“ each”

[英]TypeError: Cannot call method 'each' of undefined node.js

It's a continuation of question from here . 这是问题的一个延续,从这里 My aim is to update all the documents with minimum score. 我的目标是以最低分数更新所有文档。

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

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

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

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);
    }
);
cursor.each(function(err,doc)
{
db.collection('students').update({'_id':doc._id},{$pull:{'scores':{'score':doc.minimum}}});
});
});

Using this i am getting error 使用这个我得到错误

node app.js 
undefined

/home/oroborus/node_modules/mongodb/lib/mongodb/mongo_client.js:475
          throw err
                ^

TypeError: Cannot call method 'each' of undefined
        at /home/oroborus/hw3-1/app.js:18:8
        at /home/oroborus/node_modules/mongodb/lib/mongodb/mongo_client.js:83:5
        at /home/oroborus/node_modules/mongodb/lib/mongodb/mongo_client.js:472:11
        at process._tickCallback (node.js:415:13)

According to previous programs written by me and the post above this looks correct then still why does this error persists ? 根据我以前编写的程序以及上面的帖子,这看起来是正确的,那么为什么这个错误仍然存​​在?
[Edit] [编辑]
When i did console.dir(cursor) it said undefined. 当我做console.dir(cursor)时,它说未定义。 Why ? 为什么呢 This might be because of the asynchronous behaviour of Node.js but how do i rectify it. 这可能是因为Node.js的异步行为,但我该如何纠正。 How do i make it synchronous. 我如何使其同步。
Thanks 谢谢

collection.aggregate() is asynchronous, aggregate won't return a cursor until it's done, which is in its callback. collection.aggregate()是异步的,aggregate在完成回调之前不会返回游标。 I don't think aggregate() actually returns a cursor. 我不认为aggregate()实际上会返回游标。 The result should be in 'result' of the callback, but if it does and if you need to iterate through it, the code should be in its callback: 结果应该在回调的“结果”中,但是如果确实如此,并且如果您需要遍历它,则代码应该在其回调中:

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

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

mongoclient.connect("mongodb://localhost:27017/school",function(err,db){
  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);

        if (cursor) {
            cursor.each(function(err,doc)
            {
                db.collection('students').update({'_id':doc._id},{$pull:{'scores':{'score':doc.minimum}}});
            });     
        }


    }
  );
});

Update : Just read the document and aggregate() doesn't return a cursor (unless it's 2.6 or greater, option can be specified to return cursor): http://mongodb.github.io/node-mongodb-native/api-generated/collection.html 更新 :只需阅读文档,aggregate()不会返回游标(除非它是2.6或更高版本,可以指定选项以返回游标): http : //mongodb.github.io/node-mongodb-native/api-生成/collection.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js TypeError:无法调用未定义的方法“写入” - Node.js TypeError: Cannot call method 'write' of undefined Node.js-猫鼬路径验证失败-TypeError:无法调用未定义的方法“ validate” - Node.js - Mongoose path validation failing - TypeError: Cannot call method 'validate' of undefined 类型错误:在 node.js 中聚合 mongo 时无法调用未定义的方法“toArray” - TypeError: Cannot call method 'toArray' of undefined while aggregatein mongo in node.js (节点js)-TypeError:无法调用未定义的方法'getData' - (Node js) - TypeError: Cannot call method 'getData' of undefined Node.js:无法调用未定义的方法版本(MySql) - Node.js: Cannot call method release of undefined (MySql) node.js自定义事件:无法调用未定义的方法“ emit” - node.js custom events: Cannot call method 'emit' of undefined Node.js无法调用未定义的方法“ on”进行响应 - Node.js Cannot call method 'on' of undefined for response 无法调用未定义的node.js的方法“ forEach” - cannot call method 'forEach' of undefined node.js TypeError:当使用Heroku node.js调用pg.connect时,无法调用null的方法'query' - TypeError: Cannot call method 'query' of null - when calling pg.connect with Heroku node.js 收到TypeError:无法在mysql node.js中调用null的方法“ releaseConnection” - getting TypeError: Cannot call method 'releaseConnection' of null in mysql node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM