인프런 커뮤니티 질문&답변
안녕하세요 제로초님...게시글 lastId로 불러올때 질문입니다(무한스크롤)
작성
·
379
0
제가 제로초님 강의를 듣고 따로 프로젝트를 만들고 있는데
무한 스크롤 목록을 보여줄때
where에 조건이 하나 더 붙어야 한다면 어떻게 해주어야하나요??
제가 만드는 프로젝트는 사용자가 쓴 게시글만 목록에 뿌려주는 형식으라, where에 조건이 하나 더 들어가는데요...
초기 로딩될때는 잘 나오지만
게시글이 무한스크롤 되게끔 스크롤링하면 밑에 게시글이 나오지 않아서 질문드립니다...
router.get('/', async (req, res, next) => {
// GET /categories
try {
const where = {};
if (parseInt(req.query.lastId, 10)) {
// 초기 로딩이 아닐 때
where.id = { [Op.lt]: parseInt(req.query.lastId, 10) };
console.log(where);
}
console.log('라스트 아이디?', parseInt(req.query.lastId, 10));
const categories = await PostCategory.findAll({
where: { MemberId: req.query.memberId },
limit: 9,
order: [
['createdAt', 'DESC'], // 게시글 내림차순
],
include: [
{
model: Member,
},
{
model: Attachment,
},
],
}); // 모든 게시물 가져온다
// console.log(categories);
res.status(200).json(categories);
} catch (error) {
console.error(error);
next(error);
}
});
제가 작성한 코드입니다.
내가 쓴 게시글만 불러오고 싶어서 memberId를 쿼리값으로 불러왔습니다..
GET /categories?lastId=0&memberId=1 200
처음 렌더링할땐 이렇게 라스트 아이디 값과 멤버 아이디 값을 잘 불러오는데, 스크롤을 내리면
GET /categories?lastId=7&memberId=undefined 200 6.896 ms - 2
이렇게 멤버 아이디 값이 undefined가 나오는 것 같습니다ㅠㅠ
if (parseInt(req.query.lastId, 10)) {
// 초기 로딩이 아닐 때
// lastid 가 parseInt(req.query.lastId, 10)보다 작은...
where.id = { [Op.lt]: parseInt(req.query.lastId, 10) };
console.log(where);
}
여기에서 where.id 에 멤버 아이디를 안넣어줘서 그런건가 싶어 멤버 아이디를 넣어줬는데 그래도 500 에러가 뜨네요ㅠㅠ
혹시 여기 where.id 값에 memberid 값을 넣는 방법이 무엇일까요...? 시퀄라이즈 다중 where 처리..이런식으로 구글링해도 확실한 답을 못찾겠어서 질문드립니다ㅠㅠ
답변 2
0
0





최초로 불러올때 사용하지 않고 있다는 말씀이신가요?ㅠㅠㅠ where, 로 불러오지 않아서 그런건가요...??ㅠㅠㅠ