简体   繁体   English

removeEventListener 不工作,它不是匿名的

[英]the removeEventListener is not Working and it's not anonymous

so I have a function that submits a post and it's called after the onAuthStateChanged (firebase) I want to remove it and add it again whenever a user logs in but it's not removed and I am not getting any error所以我有一个提交帖子的函数,它在onAuthStateChanged ( firebase ) 之后调用我想删除它并在用户登录时再次添加它但它没有被删除,我没有收到任何错误

auth.onAuthStateChanged(function (user) {if (user) {
// User is signed in.
// creating posts
posts = db.collection("posts");
const { serverTimestamp } = firebase.firestore.FieldValue;
console.log(serverTimestamp)
const submitPost = async (event) => {
  event.preventDefault();

  let LastId = posts.doc("id");
  let id;
  LastId.get().then((doc) => {
    id = doc.data().lastPost;
    id++;
    id = id.toString();

    const post = {
      uid: user.uid,
      content: a.preview.innerHTML,
      createdAt: serverTimestamp(),
      id: Number(id),
    };
    console.log(post.uid);
    posts.doc(id).set(post).then(closeMD()); // puted this line here instead of outside "then" because i need the id
    LastId.update({ lastPost: Number(id) });
  });
};

submit.addEventListener("click", submitPost);
submit.removeEventListener("click" , submitPost)}

EDIT编辑

I found that the onAuthStateChanged is an observer and I didn't really understand how to work with it, I have read the documentation and saw a video by firebase but I still didn't understand how to work with it我发现onAuthStateChanged是一个observer ,我真的不明白如何使用它,我已经阅读了文档并看到了 firebase 的视频,但我仍然不明白如何使用它

I still don't know how to deal with it but I found a solution I just cloned the old button and replaced it with the cloned one just like this :我仍然不知道如何处理它,但我找到了一个解决方案,我只是克隆了旧按钮并将其替换为克隆的按钮,就像这样:

    const submit = document.getElementById("submitPost");
       const newSubmit = submit.cloneNode(true);
    submit.parentNode.replaceChild(newSubmit , submit)
    newSubmit.addEventListener("click", submitPost);

and I still want to understand how this observal works我仍然想了解这个observal是如何工作的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM