0

why is the first query not working I am using mongoose i want to e able to check for errors during query that why I want to use the first instance

import Work from './WorkModel.js'
const response = await Work.findOne({ user: userId}).exec((err, result) => {
        if (err) {
            throw new Error('Try again later');
        } else {
            console.log(odemruId);
            return result;
        }
    });

return response
//this return no value


const response= await Work.findOne({user: userId})
return response
//this actual works 

```
  • [Should You Use `exec()` With `await`?](https://mongoosejs.com/docs/promises.html#should-you-use-exec-with-await?) – chridam Nov 11 '20 at 16:18
  • @chrisdam do you mean i should not use await with exec – ukuyoma theophilus Nov 11 '20 at 16:23
  • Use it but either you do it without the callback function `(err, result) => { ... }` or ditch the async/await pattern if you want to use the callback – chridam Nov 11 '20 at 16:24
  • In other words, it's encouraged to use `exec` as `const response = await Work.findOne({ user: userId }).exec();` instead of just `const response = await Work.findOne({ user: userId });` since `exec()` gives you better stack traces. Mixing async/await with callbacks is an anti-pattern unless in the callback you return a promise. Better stick to `async/await` without the callback as the `exec()` parameter. – chridam Nov 11 '20 at 16:32
  • https://stackoverflow.com/questions/31549857/mongoose-what-does-the-exec-function-do – chridam Nov 11 '20 at 16:34

0 Answers0