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;
有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top