-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs falsely suggest exec()
for async/await
#8110
Comments
Both approaches work. We'll add a note to the docs. |
I'm not sure why you would reference both, since the I found out by means of a colleague who unknowingly wrote this since the docs suggested it. As far as I can tell I could PR docs. |
@moeriki The difference is that the return type of |
I see. Thanks for the feedback. I take back my words. Since users may have certain expectations from Promises I'd keep the |
You're right that queries are not promises: https://mongoosejs.com/docs/queries.html#queries-are-not-promises . The most meaningful difference is that if you TLDR; there are reasons to prefer using @jloveridge I'm curious to hear why you prefer a true promise over a thennable. Is it one of the reasons I listed or something else? |
So @vkarpov15 i use this approach :
It works but is it faulty? I do the same for updating, saving and deleting documents |
@Enado95 I'm not sure what you mean by 'work'. The code as written definitely needs some work - its clumsy and suppresses errors. Why do you do |
@vkarpov15 When say it works. I mean it returns a response and if there's mongo error it returns the a 500 response as well; here's my exact implementation below for deleting a record. I do the same for updating and save(): exports.deleteCompany = async (req, res) => {
const company = await Company.findByIdAndDelete(
mongoose.Types.ObjectId(req.params.id)
)
.then(data => {
if (!data)
return res.status(404).send('Company with the given id was not found')
res.send('Company successfully deleted')
})
.catch(err => {
return res.status(500).send({
message: err.message
})
})
} Since the |
This implementation for deleting a company should work fine. Not sure what you mean by "truly make this method async". |
So the same implementation for find or findOneAndUpdate would work similarly? |
find() and findOneAndUpdate() both return mongoose queries, so they should be fine as far as However, it is typically better to use |
Okay noted.
…On Sun, Sep 29, 2019 at 6:53 PM Valeri Karpov ***@***.***> wrote:
find() and findOneAndUpdate() both return mongoose queries, so they should
be fine as far as await is concerned. The exec() isn't necessary.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#8110?email_source=notifications&email_token=AGE5KHRAVXA4RD5QBJSNAZ3QME5WHA5CNFSM4IR7P3PKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD74BVYI#issuecomment-536353505>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGE5KHUKXKAOX3NTH2YK67DQME5WHANCNFSM4IR7P3PA>
.
|
A string is not an error. I would avoid throwing strings by any means necessary. Just remove the entire catch. Let your calls to |
Do you want to request a feature or report a bug?
Docs.
What is the current docs?
https://mongoosejs.com/docs/promises.html#built-in-promises
What is the expected docs?
await Model.findOne()
works just fine.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose 5.5.14
Node.js 10.16.2
The text was updated successfully, but these errors were encountered: