작성
·
386
0
첫번째 부터 순서대로post모델,user모델 passport.index의 deserializeUser부분 route.page 부분 입니다.
User is associated to User multiple times. To identify the correct association, you must use the 'as' keyword to specify the alias of the association you want to include.
위 같은 오류가 뜨는데 어떤게 문제인지 아무리 찾아봐도 모르겠습니다..
답변 1
1
routes/user.js
const express = require("express");
const router = express.Router();
const { isLoggedIn } = require("../middlewares");
const { follow, followOut, like, likeC } = require("../controllers/user");
router.post("/:id/follow", isLoggedIn, follow);
router.delete("/:id/followOut", isLoggedIn, followOut);
router.post("/:postId/like", isLoggedIn, like);
module.exports = router;
controllers/user.js
const User = require("../models/user");
const Post = require("../models/post");
exports.follow = async (req, res, next) => {
try {
const user = await User.findOne({ where: { id: req.user.id } });
if (user) {
user.addFollowings(parseInt(req.params.id, 10));
res.send("success");
} else {
res.status(404).send("no user");
}
} catch (error) {
console.error(error);
next(error);
}
};
exports.followOut = async (req, res, next) => {
try {
const user = await User.findOne({ where: { id: req.user.id } });
if (user) {
user.removeFollowings(parseInt(req.params.id, 10));
res.send("success");
} else {
res.status(404).send("no user");
}
console.log(User);
} catch (error) {
console.error(error);
next(error);
}
};
exports.like = async (req, res, next) => {
try {
const user = await User.findOne({
where: { id: req.user.id },
include: { model: Post },
});
if (user) {
user.addLikings(parseInt(req.params.postId, 10));
res.send("success");
} else {
res.status(404).send("no user");
}
} catch (error) {
console.error(error);
next(error);
}
};
exports.likeC = (req, res, next) => {};
/passport/index.js
const passport = require("passport");
const local = require("./localStrategy");
const kakao = require("./kakaoStrategy");
const User = require("../models/user");
const Post = require("../models/post");
module.exports = () => {
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findOne({
where: { id },
include: [
{
model: User, //팔로잉
attributes: ["id", "nick"],
as: "Followers",
},
{
model: User, //팔로워
attributes: ["id", "nick"],
as: "Followings",
},
{
model: User, //좋아요
attributes: ["id", "nick"],
as: "Likings",
},
],
})
.then((user) => done(null, user))
.catch((err) => done(err));
});
local();
kakao();
};
models/post.js
static associate(db) {
db.Post.belongsTo(db.User);
db.Post.belongsToMany(db.HashTag, { through: "PostHashtag" });
//좋아요 한 사람
db.Post.belongsToMany(db.User, {
foreignKey: "likePostList",
as: "Likeds",
through: "likeList",
});
}
}
models/user.js
static associate(db) {
db.User.hasMany(db.Post);
db.User.belongsToMany(db.User, {
//팔로워
foreignKey: "followingId",
as: "Followers",
through: "Follow",
});
db.User.belongsToMany(db.User, {
//팔로잉
foreignKey: "follwersId",
as: "Followings",
through: "Follow", //db.sequelize.models.Follow로 접근 가능
});
db.User.belongsToMany(db.Post, {
//좋아요 한 게시글 리스트
foreignKey: "likeIdList",
as: "Likings",
through: "likeList",
});
}
혹시 몰라서 코드 남깁니다....
3번재 사진이 안올라갔네요 만약 deserializeUser 이 부분 include를 말씀하시는거면 넣었는데도 재시작해도 오류가 뜨네요ㅜㅜㅜ