Help Pleas

Make sure that the updateValidity function is being called properly. You could try adding a console.log statement in this function to see if it is being called as expected. Make sure that the follow function is properly inserting a new record into the “following” collection. You could try checking the “following” collection in your database to see if the new record is being added as expected. If the new record is being added to the “following” collection, but the button is still not updating, it could be an issue with the $w(“#follow”).label = “Unfollow” line of code. Make sure that this line is being executed after the new record has been added to the collection.

Have rewrite it

$w.onReady(function () {
  $w("#follow").onClick(function () {
    follow();
  });
});

async function follow() {
  // Get the current user
  const currentUser = wixUsers.currentUser;

  // Get the user ID of the first user with the specified username
  const username = $w("#username").label.toString();
  const user = await wixData.query("users")
    .limit(1)
    .eq("username", username)
    .find()
    .then(results => results.items[0]);
  const userId = user._id;

  // Insert a new record into the "following" collection
  const follow = {
    followee: userId,
    follower: currentUser
  };
  await wixData.insert("following", follow)
    .then(updateValidity)
    .catch(console.log);
}

function updateValidity() {
  // Update the label of the "follow" button to "Unfollow"
  $w("#follow").label = "Unfollow";

  // Set the "onClick" handler for the "follow" button to the "unfollow" function
  $w("#follow").onClick(function () {
    unfollow();
  });
}

async function unfollow() {
  // Get the current user
  const currentUser = wixUsers.currentUser;

  // Get the user ID of the first user with the specified username
  const username = $w("#username").label.toString();
  const user = await wixData.query("users")
    .limit(1)
    .eq("username", username)
    .find()
    .then(results => results.items[0]);
  const userId = user._id;

  // Remove the record from the "following" collection
  await wixData.remove("following", { followee: userId, follower: currentUser })
    .then(updateButton)
    .catch(console.log);
}

function updateButton() {
  // Update the label of the "follow" button to "Follow"
  $w("#follow").label = "Follow";

  // Set the "onClick" handler for the "follow" button back to the "follow" function
  $w("#follow").onClick(function () {
    follow();
  });
}