Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs - Normal callback vs exec [duplicate]

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: ''}
}));
like image 416
Chrillewoodz Avatar asked Apr 08 '17 10:04

Chrillewoodz


1 Answers

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
});
like image 94
Libu Mathew Avatar answered Oct 18 '22 18:10

Libu Mathew