$('#chat').click(function () {
db.collection("chatroom").where("who", "array-contains-any", [구매자uid, 판매자uid]).get().then((result) => {
console.log(result.empty)
console.log(구매자uid)
console.log(판매자uid)
if (result.empty == true) {
//새로 채팅방 개설
var 데이터 = {
who: [내uid, 판매자uid],
판매자uid,
구매자uid,
product: 상품명,
productID,
date: new Date()
}
db.collection('chatroom').add(데이터).then(function (doc) {
console.log(doc.id);
sessionStorage.setItem('chatroom', JSON.stringify(doc.id))
채팅방id = JSON.parse(sessionStorage.getItem('chatroom'));
setTimeout(() => window.location.href = 'chatroom.html?id=' + 채팅방id, 2000);
})
}
else {
//기존 채팅방 불러오기
result.forEach((a) => {
//기존 채팅방으로 이동
if (내uid === a.data().who[0] && 판매자uid === a.data().who[1]) {
console.log(a.data().who[0]);
console.log(a.data().who[1]);
//채팅방 id 출력
db.collection("chatroom").get(a.id).then(function (doc) {
console.log(a.id)
});
기존채팅방id = a.id;
console.log(기존채팅방id);
//기존 채팅방으로 가기
setTimeout(() => window.location.href = 'chatroom.html?id=' + 기존채팅방id, 2000);
}
})
}
})
})
말씀하신대로 코드 수정했지만, db 상에 하나라도 저장된 데이터가 있으면 false를 띄웁니다