javascript 我如何使它不只是喜欢最新的职位,它会喜欢的职位,他们点击了喜欢按钮?

6ju8rftf  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(160)

我如何使它不只是喜欢最新的职位,它会喜欢的职位,他们点击了喜欢按钮?
代码:

postSearch.addEventListener("input", (ev) => {
  async function findPosts() {
    const postsRef = collection(firestore, "posts")
    const q = query(postsRef, orderBy("createdAt"));

    const querySnapshot = await getDocs(q);
    querySnapshot.forEach((post) => {

      // doc.data() is never undefined for query doc snapshots
      if (post.data().likes == undefined) {
        post.data().likes = 0
      }

      function epicTest() {
        let postData = {
          description: post.data().description,
          display_name: post.data().display_name,
          createdAt: post.data().createdAt,
          uid: post.data().uid,
          title: post.data().title,
          likes: post.data().likes + 1
        }
        console.log(post.id)
        console.log(postData)
        setDoc(doc(postsRef, post.id), postData)
        console.log("this feature hasn't been added yet")
      }
      let items = querySnapshot.docs.map(post => {
        if (post.data().title.includes(ev.target.value) || post.data().description.includes(ev.target.value)) {
          return `<div id="postBox">
      <h4 id="postName">${post.data().display_name}</h4>
      <h1 id="postTitle">${post.data().title}</h1>
      <h3 id="postDescription">${post.data().description}</h3>
      <div id="likeContainer"><ion-icon name="thumbs-up-outline" id="likeBtn" onclick="epicTest()"></ion-icon><h3 id="likeCount">${post.data().likes}</h3></div>
      </div>`

        }
      });
      items.reverse()
      postList.innerHTML = items.join('');
      if (postList.innerText == "") {
        postList.innerText = "no results found"
      }
      let likeBtn = document.querySelectorAll("#likeBtn")
      likeBtn.forEach(item => {
        item.addEventListener("click", (ev) => {
          let postData = {
            description: post.data().description,
            display_name: post.data().display_name,
            createdAt: post.data().createdAt,
            uid: post.data().uid,
            title: post.data().title,
            likes: post.data().likes + 1
          }
          console.log(post.id)
          console.log(postData)
          setDoc(doc(postsRef, post.id), postData)
          console.log("this feature hasn't been added yet")
        })
      })
    });
  }
  findPosts()
})
bfnvny8b

bfnvny8b1#

以下示例显示了如何使用函数在单击“Like”按钮时运行,请检查以下示例:

var postRef = new Firebase(firebaseURL + id);
    postRef.child('like-count').once('value', function(snapshot) {
        var currentLikes = snapshot.val() ? snapshot.val() : 0;
        postRef.update({
            'postID': id,
            'like-count': currentLikes + 1
            }, function(error) {
              if (error) {
                console.log('Data could not be saved:' + error);
              } else {
                console.log('Data saved successfully');
              }
            });
        getLikeCount(id);
    });   
}

其基本思想是将post id作为参数传递给单击like按钮时调用的函数。
还可以查看以下示例中的类似实现:

相关问题