You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- Now the handling of unknown endpoints is ordered <i>before the HTTP request handler</i>. Since the unknown endpoint handler responds to all requests with <i>404 unknown endpoint</i>, no routes or middleware will be called after the response has been sent by unknown endpoint middleware. The only exception to this is the error handler which needs to come at the very end, after the unknown endpoints handler. -->
Let's add some missing functionality to our application, including deleting and updating an individual note.
1010
+
<!--Let's add some missing functionality to our application, including deleting and updating an individual note.-->
1011
1011
1012
-
The easiest way to delete a note from the database is with the [findByIdAndRemove](https://mongoosejs.com/docs/api.html#model_Model.findByIdAndRemove) method:
1012
+
让我们为我们的应用程序添加一些目前缺失的功能,包括删除和更新单个笔记。
1013
+
1014
+
<!-- The easiest way to delete a note from the database is with the [findByIdAndRemove](https://mongoosejs.com/docs/api.html#model_Model.findByIdAndRemove) method: -->
In both of the "successful" cases of deleting a resource, the backend responds with the status code <i>204 no content</i>. The two different cases are deleting a note that exists, and deleting a note that does not exist in the database. The _result_ callback parameter could be used for checking if a resource actually was deleted, and we could use that information for returning different status codes for the two cases if we deemed it necessary. Any exception that occurs is passed onto the error handler.
1028
+
<!-- In both of the "successful" cases of deleting a resource, the backend responds with the status code <i>204 no content</i>. The two different cases are deleting a note that exists, and deleting a note that does not exist in the database. The _result_ callback parameter could be used for checking if a resource actually was deleted, and we could use that information for returning different status codes for the two cases if we deemed it necessary. Any exception that occurs is passed onto the error handler. -->
1029
+
1030
+
在删除资源的两种“成功”情况下,后端都会以状态码 <i>204 no content</i> 做出响应。这两种不同的情况分别是删除数据库中存在的笔记和删除数据库中不存在的笔记。可以使用_result_ 回调参数来检查资源是否确实已被删除,如果需要,我们可以使用这些信息来返回不同的状态码来区分这两种情况。发生的任何异常都将传递到错误处理程序。
1031
+
1032
+
1033
+
<!-- The toggling of the importance of a note can be easily accomplished with the [findByIdAndUpdate](https://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate) method. -->
1025
1034
1026
-
The toggling of the importance of a note can be easily accomplished with the [findByIdAndUpdate](https://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate)method.
In the code above, we also allow the content of the note to be edited. However, we will not support changing the creation date for obvious reasons.
1054
+
<!--In the code above, we also allow the content of the note to be edited. However, we will not support changing the creation date for obvious reasons.-->
1046
1055
1047
-
Notice that the <em>findByIdAndUpdate</em> method receives a regular JavaScript object as its parameter, and not a new note object created with the <em>Note</em> constructor function.
1056
+
在以上代码中,我们允许笔记内容的修改。然而,出于明显的原因,我们不允许修改笔记的创建日期。
1048
1057
1049
-
There is one important detail regarding the use of the <em>findByIdAndUpdate</em> method. By default, the <em>updatedNote</em> parameter of the event handler receives the original document [without the modifications](https://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate). We added the optional <code>{ new: true }</code> parameter, which will cause our event handler to be called with the new modified document instead of the original.
1058
+
<!-- Notice that the <em>findByIdAndUpdate</em> method receives a regular JavaScript object as its parameter, and not a new note object created with the <em>Note</em> constructor function. -->
1050
1059
1051
-
After testing the backend directly with Postman and the VS Code REST client, we can verify that it seems to work. The frontend also appears to work with the backend using the database.
<!-- There is one important detail regarding the use of the <em>findByIdAndUpdate</em> method. By default, the <em>updatedNote</em> parameter of the event handler receives the original document [without the modifications](https://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate). We added the optional <code>{ new: true }</code> parameter, which will cause our event handler to be called with the new modified document instead of the original. -->
<!-- After testing the backend directly with Postman and the VS Code REST client, we can verify that it seems to work. The frontend also appears to work with the backend using the database. -->
1067
+
1068
+
在使用 Postman 和 VS Code REST 客户端直接测试后端之后,我们可以确认它似乎可以正常工作。前端似乎也能够与使用数据库的后端正常工作。
1052
1069
1053
1070
You can find the code for our current application in its entirety in the <i>part3-5</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part3-notes-backend/tree/part3-5).
0 commit comments