![[TIL] Blog Login, SignUp & additional features](https://cdn.hashnode.com/res/hashnode/image/upload/v1681907624875/36f92103-cb91-4c3d-82e3-0e8d4159e1b8.png)
TIL
Issues encountered
nested try-catch refactoring
nested router 처리하기: https://stackoverflow.com/questions/25260818/rest-with-express-js-nested-router
What I tried
- 처음에는 mongoDB의 데이터들을 지우고 업데이트할 수 있는 deleteOne()과 updateOne() functions를 try-catch문 안에 넣어서 해당 함수 처리가 실패하였을 때 catch{}로 넘어가는 식으로 만들어서 nested try-catch문을 사용하게 되었었다. => 그러나, 위 두 함수는 Promise 형태이기 때문에 뒤에
.catch()를 통해 에러를 바로 잡을 수 있다.
try {
const existingPost = await Posts.findOne({ userId, _id: _postId }).exec();
if (!existingPost)
return res.status(403).json({ errorMessage: "게시글 수정의 권한이 존재하지 않습니다.",});
try {
await Posts.updateOne({ userId, _id: _postId }, { $set: { title, content } });
res.status(200).json({ message: "게시글을 수정하였습니다." });
} catch (error) {
console.error(error);
return res.status(401).json({
errorMessage: "게시글이 정상적으로 수정되지 않았습니다.",});
}
} catch (error) {
console.error(error);
res.status(400).json({ errorMessage: "게시글 수정에 실패하였습니다." });
}
const router = express.Router({ mergeParams: true });이를 각 하위 라우터 파일들에 넣어주면 index.js에서 경로 처리를 할 수 있다.const express = require("express"); const router = express.Router(); const postsRouter = require("./posts.js"); const usersRouter = require("./users.js"); const commentsRouter = require("./comments.js"); router.use("/users", [usersRouter]); router.use("/posts", [postsRouter]); router.use("/posts/:_postId/comments", [commentsRouter]); module.exports = router;
How I resolved the issues
try {
const existingPost = await Posts.findOne({userId, _id: _postId,}).exec();
if (!existingPost)
return res.status(403).json({
errorMessage: "게시글 수정의 권한이 존재하지 않습니다.",});
await Posts.updateOne(
{ userId, _id: _postId },
{ $set: { title, content } }
).catch((error) => { // 여기서 catch()로 처리
console.error(error);
res.status(401).json({
errorMessage: "게시글이 정상적으로 수정되지 않았습니다.",
});
});
res.status(200).json({ message: "게시글을 수정하였습니다." });
} catch (error) {
console.error(error);
res.status(400).json({ errorMessage: "게시글 수정에 실패하였습니다." });
}
What I newly learned
bcrypt: to make password more secure by converting the user input password into the hashed passwordhow to deal with the nested router
스터디 하면서 배운거
React의 useState나 component 안에서 state 변경 값이 유지되는 것이 내부적으로 클로저를 사용해서 가능하다! 클로저를 통해 스코프 내에서 변수의 값이 유지된다.
얕은 복사 깊은 복사
- object 자체는 private이지만 object의 값들은 public이다
object의 값들 하나하나가 실제 값이 저장되어 있는 address를 가리키기 때문에 원본 object의 복사본이 값을 변경하는 것이 가능하다
depth가 2 이상이면 또 변경 불가
깊은 복사는 recursion으로 구현해야함
- lodash: a JavaScript library that works on the top of underscore.js. It helps in working with arrays, strings, objects, numbers, etc. It provides us with various inbuilt functions and uses a functional programming approach which that coding in JavaScript easier to understand because instead of writing repetitive functions,
What to learn next
Change MongoDB to MySQL
Learn Sequelize
![[코테] 그리디 문제 - 무지의 먹방 라이브](https://cdn.hashnode.com/res/hashnode/image/upload/v1712215455263/1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png)
![[코테] Bfs 토마토](https://cdn.hashnode.com/res/hashnode/image/upload/v1709032619170/70056896-c857-444b-9c99-45bfcb466806.png)
![[코테] Dfs 문제 유형 - 그래프 내에서 구분하여 카운트 하기](https://cdn.hashnode.com/res/hashnode/image/upload/v1709019361383/b0585d72-c808-4169-83a9-2724f312e927.png)
![[코테] DFS vs BFS](https://cdn.hashnode.com/res/hashnode/image/upload/v1708971211123/71f9386c-6a62-43b2-a602-4d084c24d6cf.png)
![[코테] 여행경로](https://cdn.hashnode.com/res/hashnode/image/upload/v1708971251412/27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png)