$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";
}
thanks for your help, i still have problem with getFollowing after updateValidity
function getFollowing() {
let follower = wixUsers.currentUser.id
let followed = $w('#dynamicDataset').getCurrentItem();
wixData.query('following')
.eq('followed', followed)
.find();
if (follower == follower) {
$w("#follow").label = "unfollow"
} else {
$w("#follow").label = "follow"
}
}
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();
});
}
There was an error in your code when I tried it, but I fixed it when entering the reference data and it worked, but after the data was entered and the validation was updated to make sure the validation was logged it still didn’t run after I reloaded the page.
This is the code I use.
$w.onReady(function () {
$w('#follow').onClick(function () {
follow();
});
});
function follow() {
let follower = wixUsers.currentUser.id
let followed = $w('#dynamicDataset').getCurrentItem();
wixData.insert('following', {
'followed': followed,
'follower': follower
})
.then(updateValidity)
.catch((err) => {
console.log("err");
});
}
function getFollowing() {
let follower = wixUsers.currentUser.id;
let followed = $w('#dynamicDataset').getCurrentItem();
wixData.query('following')
.eq('followed', followed)
.find()
.then(result => {
if (result.items.length > 0 && result.items[0].follower === follower) {
$w("#follow").label = "unfollow";
} else {
$w("#follow").label = "follow";
}
})
.catch(error => {
console.log(error);
});
}
function updateValidity() {
$w("#follow").label = "Unfollow";
}
Make sure that the follow() function is being called when you expect it to be. You can use the browser’s developer console to check if the function is being called and whether it is executing correctly. Check if there are any errors being thrown in the browser’s developer console.