6

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: ''}
}));
Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
  • This isn't a question about NodeJS. It's a question about whatever library `Product` is part of and what difference, if any, there is between two different ways of using that library's API. If you don't tell us what library it is, we can't help you. We could *guess* what it is, but requiring guessing isn't a great idea. (Also be sure you read the library API documentation carefully as part of your due diligence research before posting, and quote anything relevant.) – T.J. Crowder Apr 08 '17 at 10:17
  • @T.J.Crowder Product is just a mongodb model. – Chrillewoodz Apr 08 '17 at 10:19
  • @T.J.Crowder Updated question. – Chrillewoodz Apr 08 '17 at 10:20
  • I don't know about mongodb but in most other database libraries something like `.exec()` allows you to dynamically create queries. For example something like: `var query = Product(select); if (something) {query.where(condition)}; query.exec(callback)` – slebetman Apr 08 '17 at 10:23
  • Duplicate of [*Mongoose - What does the exec function do?*](http://stackoverflow.com/questions/31549857/mongoose-what-does-the-exec-function-do) @slebetman - I already voted to close as unclear, if you would do the honors (provided you agree). – T.J. Crowder Apr 08 '17 at 10:40
  • @T.J.Crowder: Not quite duplicate. The answers to the other question all missed the most important use-case for `.exec()` - to use method chaining to create queries before submitting the query to the database. As such it allows you to do things like creating queries asynchronously, create queries based on configurable input, create queries algorithmically etc. – slebetman Apr 08 '17 at 14:58

1 Answers1

4

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
});
Community
  • 1
  • 1
Libu Mathew
  • 2,976
  • 23
  • 30
  • If it's a duplicate (and good find, it is), don't answer, comment to that effect linking to the dupetarget so people with vote-to-close rights can close it. – T.J. Crowder Apr 08 '17 at 10:40
  • @T.J.Crowder What actually I'm posted is an example. You can't find it on the link. For understanding the difference between callback and exec I added the link. – Libu Mathew Apr 08 '17 at 10:45
  • @LibuMathew: SO's definition of a "duplicate" is "Do the answers there answre this question:?" That's absolutely true of the question you pointed to (again, well done). Effective use of duplicates is an important part of the SO model. If you think the question should be *better* answered than the answers there, but all means post an answer -- there. Anyway, happy coding. – T.J. Crowder Apr 08 '17 at 10:47