Nodejs - Normal callback vs exec
10,948
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
});
Comments
-
Chrillewoodz almost 2 years
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 aboutexec
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: ''} }));