不能使用mongodb调用nodejs中undefined的方法''update'

时间:2015-01-21 07:57:35

标签: node.js mongodb

这是我的mongodb示例代码,其中包含nodejs,



        var MongoClient = require('mongodb').MongoClient;
        MongoClient.connect('mongodb://localhost:27017/weather',function(err,db){
        if(err) throw err;
        db.collection('data').find().sort([["State",'1'],["Temprerature",'-1']]).toArray(function(err, docs){
        if(err) throw err;
        
        var res = {};
        var state = "";
        var month_highs=[];
        
        for (var i=0; i<docs.length; i++){
        var temperature = docs[i]['Temperature'];
        
        //we are transitioning to a new state
        if(state != docs[i]["State"]){
        month_highs.push(docs[i]);
        }
        state = docs[i]["State"];
        
        }
        
        // number of responses we have received
        
        var numCallbacks = 0;
        
        for(i=0; i<month_highs.length; i++){
        	db.collection['date'].update(month_highs[i], {"$set": {'month_high':true} }, function(err,updated){
        	if(err) throw err;
        	console.log("Update " + updated + " document month high");
        	if(++numCallbacks == month_highs.length){
        	return db.close();
        	}
        	} );
        
        }
        });
    
    });
&#13;
&#13;
&#13;

我正在跑步时变得像

一样
        throw message;
             ^
TypeError: Cannot call method 'update' of undefined

如何恢复此错误?

1 个答案:

答案 0 :(得分:1)

我的猜测是db.collection['date']实际上应该是db.collection('data'),就像您之前在代码段中所做的那样。

此外,您在第4行有拼写错误:Temprerature应为Temperature