Pregunta

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;
¿Fue útil?

Solución

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.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top