Saturday, 17 August 2013

How do I test for an empty javascript object?

How do I test for an empty javascript object?

I'm just starting to play around with mongoose.find() and it's returning
an empty object. I'm trying to test for this and create and save a new one
if that's the case.
// Create or init
Foo = require('./models/foo.js');
Foo.find({ name: process.env.FOO_NAME }, function(err, foo) {
if (err) throw err;
console.log(foo); // prints: {}
if (!foo) {
// Create the new one
foo = new Foo({ name: process.env.FOO_NAME });
foo.save();
}
foo.run();
});
What's the best way to test for an empty object?

No comments:

Post a Comment