Domanda

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;
È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top