Nodejs - Normal callback vs exec

Chrillewoodz

I've been learning nodejs for the last couple of days and I stumbled upon something that I can't find any good explanations to.

Basically it's about exec vs a normal callback, i.e. (err, res) => {}, like this:

Product.find({}).exec((err, products) => {});

Product.find({}, (err, products) => {});

I find more examples that use exec, but when I read about exec I can't really understand why. They both seem to be doing the same thing to me.

So, my question is, should I be using one over the other, and if so, why?

EDIT:

Just to make things clear, Product is a MongoDB model/schema. Like this:

const Product = mongoose.model('Product', new Schema({
  title: {type: String, default: ''},
  description: {type: String, default: ''},
  price: {type: Number, default: ''}
}));
Libu Mathew

Just refer the following answer Mongoose - What does the exec function do?

exec normally used for executing dynamically created queries.

The following is a simple code that gives an idea where you can use exec.

employee.find({}, function (err, docs) {
    // statements
});

employee.find({}).populate("designation").exec(function (err, docs) {
    // statements
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related