//realtime listener for updates to the database. this will take the doc ID from firestore and pass it to the renderList function
db.collection('whispers').where("sender", "==", userID).orderBy('time','asc').onSnapshot(snapshot => {
let changes = snapshot.docChanges();
changes.forEach(change => {
if (change.type === "added") {
renderList(change.doc,whisperList);
}
if (change.type === "removed") {
let li = document.getElementById(change.doc.id);
console.log(li);
li.remove();
}
});
});
//renderlist will create a list item in the HTML
function renderList(doc,tabList){
let li = document.createElement('li');
let message = document.createElement('span');
li.setAttribute('id', doc.id);
let x = doc.data().message;
li.appendChild(message);
tabList.insertBefore(li,tabList.childNodes[0]);
}
The above code should remove a list item based on an event listener from a Firebase Firestore database. Upon removal of data from the database, an event listener activates the code above. It should pick the ID of the HTML element and removes it. The console log part logs the list data correctly so there does exist a list item with that ID, but the following line to remove this doesn't produce any action. How can I ensure the item is removed? Strangely, when I take the logged ID and manually try to remove it from the console, it works.