Back

I'm addressing an issue with the 'remove like' functionality.

  • 0
  • Databases
  • Functions
anant
18 Oct, 2024, 14:54

async removeUserLike(userId, postId) { try { console.log(userId, postId); const searchLikedUser = await this.getAllByQueries( [Query.equal("post_id", postId), Query.equal("user_id", userId)], config.appwriteLikeId );

TypeScript
if(searchLikedUser.documents.length > 0){
  const user = searchLikedUser.documents[0].$id;
  return await this.databases.deleteDocument(
      config.appwriteDatabaseId,
      config.appwriteLikeId,
      user
    );
  }
  console.log("liked id removed successfully");
} catch (error) {
  console.log("Error while removing like:", error.message);
}

}

TL;DR
Developers are facing issues with the 'remove like' functionality on appwrite. The solution involves fetching the current likes, identifying the like to be removed, and pushing the updated data back. It was noted that appwrite does not support updating array attributes directly. A code adjustment was suggested to improve the removal process.
hamed
18 Oct, 2024, 15:16

some adjustment

TypeScript
async removeUserLike(userId, postId) {
  try {
    console.log(userId, postId);
    const searchLikedUser = await this.getAllByQueries(
      [
        Query.equal("post_id", postId), 
        Query.equal("user_id", userId)
      ],
      config.appwriteLikeId
    );

    if (searchLikedUser.documents.length > 0) {
      const user = searchLikedUser.documents[0].$id;
      await this.databases.deleteDocument(
        config.appwriteDatabaseId,
        config.appwriteLikeId,
        user
      );
      console.log("liked id removed successfully");
      return true;  // To confirm the action was successful.
    } else {
      console.log("No like found for the user and post.");
      return false;  // Indicates that no like existed.
    }
  } catch (error) {
    console.log("Error while removing like:", error.message);
  }
}
hamed
18 Oct, 2024, 15:19

I had a look at the code, and it seems like the flow should work, but I'm wondering what specific issue you're encountering¿

anant
18 Oct, 2024, 15:37

When i want to remove the likes from appwrite, it doesn't remove at all

hamed
18 Oct, 2024, 15:54

As far as I know, appwrite doesn't support updating array attributes directly.

hamed
18 Oct, 2024, 15:55

typically, you would need to fetch the current likes, perform the necessary operations (like removing the desired like), and then push the updated data back to the document

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more