Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Mongoose save async?

New to mongoose & node.

I can't figure out if mongoose document.save method is async. I'm assuming that it is, since it works even when not connected. Is there a way to tell when the document is actually saved (callback)?

like image 594
hba Avatar asked Oct 15 '13 22:10

hba


People also ask

Is Mongoose connect asynchronous?

The connect() method provided by the Mongoose supports both JavaScript promises and async-await syntax.

Should I use async-await with Mongoose?

You are most likely to accidentally re-execute queries in this way when mixing callbacks with async/await. This is never necessary and should be avoided. If you need a Query to return a fully-fledged promise instead of a thenable, you can use Query#exec().

Is Mongoose save a promise?

save() is a method on a Mongoose document. The save() method is asynchronous, so it returns a promise that you can await on.

What is Save () in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.


1 Answers

yep, it's async. You can use the 'error' parameter to see if there was an error during saving.

http://mongoosejs.com/docs/api.html#model_Model-save

like image 167
tldr Avatar answered Sep 19 '22 21:09

tldr