๐ Today I Learned
- Youtube API ์์ ํ๊ธฐ
→ JWTํ ํฐ ํ์ํ API ์์ฃผ๋ก
๐ข Today's error
Error 1. Cannot destructure property
// ์์ ์ญ์
app.patch('/videos/delete/:videoIdx', jwtMiddleware, video.deleteVideo);
- ๋ก๊ทธ์ธ API๋ฅผ ์คํํ ๋ ๋ฐ๊ธํ JWTํ ํฐ์ ์ฌ์ฉํ๋ API์ธ๋ฐ, Route์์ jwtMiddleware๋ฅผ ์ถ๊ฐํ์ง ์์ ๋ฐ์ํ ์๋ฌ์ด๋ค.
Error 2. ReferenceError : connection is not defined
- videoDao.videoAccessCheck์์ Connection pool์ ์์ฑํ์ง ์์์ ๋ฐ์ํ ์๋ฌ์ด๋ค.
- ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐ๋
Connection
์ ๋ง๋ ํPool
์ ๋ณด๊ดํ์๋ค๊ฐ ํ์ํ ๋ Pool์์ Connection์ ๊ฐ์ ธ๋ค ์ฌ์ฉํ๋ ์๋ฆฌ์ด๋ฏ๋ก,
→Provider
๋Service
์์ Connection์ ๋๊ฒจ์ฃผ์ง ์์ ๊ฒฝ์ฐ์๋Dao
์์ Connection์ ๊ฐ์ ธ์์ผ ์ฟผ๋ฆฌ๊ฐ ์๋ํ๋ค.
์์ฑ๋ ์์ ์ญ์ API
videoRoute.js
app.delete('/videos/delete/:videoIdx', jwtMiddleware, video.deleteVideo);
videoController.js
exports.deleteVideo = async function(req, res){
const videoIdx = req.params.videoIdx;
const userIdxFromJWT = req.verifiedToken.userIdx;
console.log(`๋ฐ์์จ ์ธ๋ฑ์ค : ${userIdxFromJWT}`);
if (!videoIdx){
return res.send(errResponse(baseResponse.VIDEO_VIDEOIDX_EMPTY));
}
const [videoAccessCheck] = await videoDao.videoAccessCheck(videoIdx);
if (videoAccessCheck[0].userIdx !== userIdxFromJWT){
return res.json({
isSuccess : false,
code : 301,
message : "์์ ์ญ์ ๊ถํ์ด ์์ต๋๋ค"
});
}
try{
await videoDao.deleteVideo(videoIdx);
res.json({
isSuccess : true,
code : 200,
message : "์์ ์ญ์ ์ฑ๊ณต"
});
} catch (err){
logger.error(`App - DeleteVideo Query error\n : ${JSON.stringify(err)}`);
return false;
}
}
videoDao.js
async function videoAccessCheck(videoIdx){
const connection = await pool.getConnection(async (conn) => conn);
const videoAccessCheckQuery = `
select userIdx
from Video
where videoIdx = ?;
`;
const videoAccessCheckRow = await connection.query(
videoAccessCheckQuery,
videoIdx
);
connection.release();
return videoAccessCheckRow;
}
๐ Comment
๊ธฐ๋ณธ์ ์ธ ๋ถ๋ถ ์ ๊ฒฝ์ฐ๊ธฐ
'TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL] 2021-08-29 (1) | 2021.08.30 |
---|---|
[TIL] 2021-08-27 (0) | 2021.08.28 |
[TIL] 2021-08-25 (2) | 2021.08.26 |