Try out
$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() {
$w("#follow").label = "Unfollow";
}