Mongoose has a save function that allows you to save a JSON object to the database. The userβs input will be converted into JSON format via our body-parser middleware.
One can need to construct a new instance of our model that we established earlier to save the data into the database. The userβs input will be passed into this instance. Only need to enter the command βsaveβ once we have it.
On a database save, Mongoose will return a promise. When the save to the database is complete, a promise is returned. Either this save will complete successfully or it will fail. A promise has two techniques for dealing with both of these eventualities.
If the database saves are successful, the promise will return to the. then section. In this situation, weβd like to send a text message to the user informing them that their data has been saved to the database.
If it fails, it will return to its promises. catch segment. In this scenario, weβd like to send the user a text message informing them that their data was not saved to the database. It is also recommended that the status code returned be changed from the normal 200 to 400. The operation failed with a status code of 400.