why like does not change in real time in UserProfile.vue

Viewed 52

post.vue code using socket ......

  this.socket.on('add-like', like => {
    this.$store.commit('addLike', like)
  })

  this.socket.on('remove-like', like => {
    this.$store.commit('removeLike', like)
  })

Posts.vue code component ........

<my-post
        :image="'http://localhost:3000/static/' + post._id + '.PNG'"
        :name="post.text"
        :id="post._id"
        :commentsArr="post.comments"
        :likeNumber="post.likes.length"
        :likesArr="post.likes"
      >
      </my-post>

USerProfile.vue code component........

<my-post
        :image="'http://localhost:3000/static/' + post._id + '.PNG'"
        :name="post.text"
        :id="post._id"
        :commentsArr="post.comments"
        :likeNumber="post.likes.length"
        :likesArr="post.likes"
      >
      </my-post>

vuex.js code addLike mutations...........

addLike: (state, like) => {
      let posts = state.posts;
      for (let i = 0; i < posts.length; i++) {
        if (posts[i]._id == like.post) {
          if (posts[i].likes.findIndex((el) => el._id === like._id) === -1) {
            posts[i].likes.push(like);
          }
        }

      }

vuex.js code addLike mutations...........

addLike: (state, like) => {
      let posts = state.posts;
      for (let i = 0; i < posts.length; i++) {
        if (posts[i]._id == like.post) {
          if (posts[i].likes.findIndex((el) => el._id === like._id) === -1) {
            posts[i].likes.push(like);
          }
        }

      }

vuex.js code addLike mutations...........

addLike: (state, like) => {
      let posts = state.posts;
      for (let i = 0; i < posts.length; i++) {
        if (posts[i]._id == like.post) {
          if (posts[i].likes.findIndex((el) => el._id === like._id) === -1) {
            posts[i].likes.push(like);
          }
        }

      }

here socket and mongoose and express..................................................

socket.on('add-remove-like', (postId) => {
     
      db.likeSchema.findOne({$and: [{post: postId.postID ,user: socket._id}]})
      .exec((err, result) => {
        if (err) {
          throw err
        } if (result !== null) { // pull like
          db.likeSchema.findOne({ $and: [{ post: postId.postID }, { user: socket._id }] }).exec((err, like1) => {
            db.likeSchema.findOneAndRemove({ $and: [{ post: postId.postID }, { user: socket._id }] }).exec((err, rs) => {
              db.postSchema.findOneAndUpdate({ _id: postId.postID },
                 { $pull: { likes:  mongoose.Types.ObjectId(like1._id.toString())
               } })
              .populate('owner')
              .exec((err, post) => {
                console.log('0000 ', like1) 
                let friendList = post.owner.friends
                for (let conn of array_of_connection) {
                  if (friendList.indexOf(conn._id) !== -1) {
                    io.to(conn.id).emit("remove-like", like1)
                  }
                }    
              })
            })
          })
        } if (result == null) {
          const newLike = new db.likeSchema();
          newLike.post = postId.postID;
          newLike.user = socket._id
          newLike.save((err, like) => {
            db.postSchema
              .findOneAndUpdate({ _id: postId.postID }, { $push: { likes: like._id } })
              .populate('owner')
              .exec((err, post) => {
                let friendList = post.owner.friends
                for (let conn of array_of_connection) {
                  if (friendList.indexOf(conn._id) !== -1) {
                    io.to(conn.id).emit("add-like", like)
                  }
                }    
              })
             
          });
        }
      })

    })
0 Answers
Related