수업 매우 매우 잘 듣고있습니다.
질문이 있어 남깁니다.
이 부분은 chatroom.html에서 '안삼' 적고 전송버튼 누를때 firebase 메세지 컬렉션에 추가하는 부분입니다.
db.collection("chatroom")
.where("who", "array-contains", 내uid)
.get()
.then((result) => {
result.forEach((a) => {
var template = `<li class="list-group-item">
<h6>${a.data().product}</h6>
<h6 class="text-small">${a.id}</h6>
</li>`;
$(".chat-list").append(template);
$(".list-group-item").click(() => {
chatRoomId = $(this).children(".text-small").text();
console.log("채팅방 선택됨", chatRoomId);
});
});
});
var chatRoomId;
//send 누를 시 채팅 내용 남기기
$("#send").click(() => {
var 데이터 = {
content: $("#chat-input").val(),
date: new Date(),
uid: 내uid,
};
db.collection("chatroom")
.doc(chatRoomId)
.collection("messages")
.add(데이터)
.then(() => {
console.log("채팅 추가함");
});
});
------여기까지 제가 수업들으면서 짠 코드인데 arrow function 으로 짯을때 chatRoomId를 가져오지 못하더라구요.
한참을 작성하신 코드와 비교하며 뭐가 다른가 보다 보다,, 결국 찾은게 ()=>{} 이걸 function(){}으로 바꿨더니 가져오더라구요..
arrow function에서는 this를 다르게 처리 해줘야하는건가요? 아니면 못받으니 그냥 function을 써야하나요?
$(".list-group-item").click(() => {
chatRoomId = $(this).children(".text-small").text();
console.log("채팅방 선택됨", chatRoomId);
왜 arrow function 은 안되고, function(){}은 되나요...?