0

This is my sample code of mongodb with 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();
         }
         } );
        
        }
        });
    
    });

while am running am getting err like

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

How do i recover this error?

Richa
  • 3,261
  • 2
  • 27
  • 51
kamatchi
  • 86
  • 2
  • 13

1 Answers1

1

My guess is that db.collection['date'] should in fact be db.collection('data') as you have previously done earlier in the code snippet.

Also, you have a typo in the 4th line: Temprerature should be Temperature

mscdex
  • 104,356
  • 15
  • 192
  • 153