-
글쓴이글
-
2022년 3월 9일 10:57 #29090
서정우참가자수업때 말씀하신 로그인한 계정이 글 작성자와 일치하지 않으면 게시물의 수정 삭제 버튼이 보이지 않게하는
업데이트를 진행하고 있었는데,(패스포트 하단에)
app.get("/list", function (req, res) {
db.collection("post")
.find()
.toArray(function (err, result) {
console.log(typeof result[0].writerId); //object
res.render("list", { posts: result, userId: req.user._id });
console.log(typeof req.user._id); //object
});
});이렇게 작성해 로그로 비교할 값들의 자료형을 미리 확인하고,
list.html에서 (넌적스 입니다)
...
<div class="container mt-3">
<ul class="list-group">
{%for val in posts%}
<li class="list-group-item">
<p>글번호 : {{val._id}}</p>
<h4 class="goToDetail">{{val.title}}</h4>
<p>{{val.description}}</p>
<p>{{val.dateNow}}</p>
<p>작성자명: {{val.writerName}}</p><p>작성자 아이디 : {{val.writerId}}</p>
<p>현재 유저 아이디 : {{userId}}</p>{% if userId == val.writerId %}
<button class="delete btn btn-info" data-id="{{val._id}}">삭제</button>
<button class="edit" onclick='location.href="/edit/{{val._id}}"'>수정</button>
{% else %}{% endif %}
<button class="edit" onclick='location.href="/detail/{{val._id}}"'>상세 페이지 이동</button>
</li>
{% endfor %}
</ul>
</div>
...이런식으로 코드를 작성했는데
사진과 같이 고유 아이디(자동으로 생성되는 오브젝트 아이디)(게시글이 작성자명 test)가 일치하는 것도 버튼 표시가 안됩니다.
2022년 3월 9일 20:19 #29121
codingapple키 마스터object라고 나오는건 아마 ObjectId 같은데 이걸 문자로 바꾼 다음에 ejs 파일로 보내봅시다 그래야 등호로 비교가능할듯요
2022년 3월 9일 21:56 #29131
서정우참가자해결했습니다만
app.get("/list", function (req, res) {
db.collection("post")
.find()
.toArray(function (err, result) {
result.forEach(e=>{
e.writerId=e.writerId.toString();
})
console.log(result);
res.render("list", { posts: result, userId:req.user._id});
});
});이거말고는 해결책이 잘 안떠오르는데 이게 제일 베스트일까요?
반복문을 쓰고 싶지 않아
app.get("/list", function (req, res) {
db.collection("post")
.find()
.toArray(function (err, result) {
// result.forEach(e=>{
// e.writerId=e.writerId.toString();
// })
// console.log(result);
res.render("list", { posts: result, userId:mongoose.Types.ObjectId(req.user._id)});
});
});mongoose.Types.ObjectId(req.user._id)로 로그인한 사용자 몽고디비 자체생성 _id를 ObjectId로 바꿔
그냥 오브젝트아이디 대 오브젝트 아이디로 비교해서 볼려했으나
이건 또 안되더라구요
2022년 3월 10일 09:42 #29151
codingapple키 마스터mongoose 저건 유저의 objectid 1개만 바꿔줄 것 같은데요 반복문쓰는게 맞을 듯 합니다
$toString인가 이런 연산자 쓰면 db에 있던거 가져올 때 문자로 변환해서 가져올 수도 있을걸요
-
글쓴이글
- 답변은 로그인 후 가능합니다.