강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

Baekgyu Koh님의 프로필 이미지
Baekgyu Koh

작성한 질문수

Node.js 교과서 - 기본부터 프로젝트 실습까지

sns 강의 수강 중 질문있습니다.

작성

·

286

0

게시글 업로드 구현하기 까지 수강해서 아래 사진까지 구현하였습니다. 

이 상태에서 회원가입 하고 그 아이디로 로그인하면 아래같은 메시지가 뜨는데 왜 그런건가요??

router index 파일 입니다.

router.get("/", async (req, res, next) => {
await Post.findAll({
include: {
model: User,
attributes: ["id", "nick"],
},
})
.then((posts) => {
res.render("index", {
title: "NodeBird",
twits: posts,
user: req.user,
loginError: req.flash("loginError"),
});
})
.catch((err) => {
console.error(err);
next(err);
});
});

module.exports = router;

index pug 파일입니다.

extends layout

block content
.timeline
if user
div
form#twit-form(action='/post' method='post' enctype='multipart/form-data')
.input-group
textarea#twit(name='content' maxlength=140)
.img-preview
img#img-preview(src='' style='display: none;' width='250' alt='미리보기')
input#img-url(type='hidden' name='url')
div
label#img-label(for='img') 사진 업로드
input#img(type='file' accept='image/*')
button#twit-btn.btn(type='submit') 짹짹
.twits
form#hashtag-form(action='/post/hashtag')
input(type='text' name='hashtag' placeholder='태그 검색')
button.btn 검색
for twit in twits
.twit
input.twit-user-id(type='hidden' value=twit.user.id)
input.twit-id(type='hidden' value=twit.id)
.twit-author= twit.user.nick
-const follow = user && user.Followings.map(f => f.id).includes(twit.user.id);
if user && user.id !== twit.user.id && !follow
button.twit-follow 팔로우하기
.twit-content= twit.content
if twit.img
.twit-img
img(src=twit.img alt='섬네일')
script.
if (document.getElementById('img')) {
document.getElementById('img').addEventListener('change', function (e) {
var formData = new FormData();
console.log(this, this.files);
formData.append('img', this.files[0]);
var xhr = new XMLHttpRequest();
xhr.onload = function () {
if (xhr.status === 200) {
var url = JSON.parse(xhr.responseText).url;
document.getElementById('img-url').value = url;
document.getElementById('img-preview').src = url;
document.getElementById('img-preview').style.display = 'inline';
} else {
console.error(xhr.responseText);
}
};
xhr.open('POST', '/post/img');
xhr.send(formData);
});
}
document.querySelectorAll('.twit-follow').forEach(function (tag) {
tag.addEventListener('click', function () {
var isLoggedIn = document.querySelector('#my-id');
if (isLoggedIn) {
var userId = tag.parentNode.querySelector('.twit-user-id').value;
var myId = isLoggedIn.value;
if (userId !== myId) {
if (confirm('팔로잉하시겠습니까?')) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
if (xhr.status === 200) {
location.reload();
} else {
console.error(xhr.responseText);
}
};
xhr.open('POST', '/user/' + userId + '/follow');
xhr.send();
}
}
}
});
});

답변 1

0

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

map of undefined이므로 user.Followings가 undefined라는 뜻입니다. Followings가 undefined인 이유는 라우터(게시글 가져오거나 리트윗 가져오는 라우터)에서 include User as Followings를 하지 않았을 확률이 높습니다.

Baekgyu Koh님의 프로필 이미지
Baekgyu Koh

작성한 질문수

질문하기