0

I have a simple method as follows to write to a mongodb database. It uses the Mongojs module. Everything seems fine with the exception of the last line. I'm getting "TypeError: Cannot call method 'save' of undefined" in the console. What could the problem be?

var db = require('mongojs').connect('mongodb://foo:bar@something.com:10027/foobar');

var saveImage = function(file, usr) {

    var imagesCollection = db.collection('Images');
    var Image = {};
    Image.imageFileName = file;
    Image.user = usr;
    Image.date = new Date();
    db.imagesCollection.save(Image);

}

exports.saveImage = saveImage;
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
hughesdan
  • 3,019
  • 12
  • 57
  • 80

2 Answers2

2

Try to change db.imagesCollection.save(Image) to imagesCollection.save(Image).

"TypeError: Cannot call method 'save' of undefined" means on that line something that has a method of save is undefined. Next try to console.log some variables to understand where is the problem.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
0

The error is telling you that db is an object and does not contain a definition of imagesCollection.

db.imagesCollection.save(Image);

add debugger right above db.imagesCollection.save(Image);

and you'll be able to see that imagesCollection is not currently a property of db.