해쉬테그
해쉬태그 추출
//게시글 등록 POST /post
router.post("/", isLoggedIn, upload.none(), async (req, res, next) => {
try {
//해쉬태그 추출
const hashtags = req.body.content.match(/#[^\s#]+/g);
const post = await Post.create({
content: req.body.content,
UserId: req.user.id,
});
if (req.body.image) {
if (Array.isArray(req.body.image)) {
const images = await Promise.all(
req.body.image.map((image) => Image.create({ src: image }))
);
await post.addImages(images);
} else {
const image = await Image.create({ src: req.body.image });
await post.addImages(image);
}
}
const fullPost = await Post.findOne({
where: { id: post.id },
include: [
{ model: Image },
{ model: Comment },
{ model: User, attributes: ["id", "nickname"] },
{ model: User, as: "Likers", attributes: ["id"] },
],
});
res.status(201).json(fullPost);
} catch (e) {
console.error(e);
next(e);
}
});해쉬태그가 있다면 해쉬태그를 hashtags태이블에 저장
게시글 업로드 완성코드
Last updated