Hello, wix coders. i need help to fixed my code for insert users to refrance field with this code:
$w.onReady(function () { $w("#follow").onClick(function () { follow(); }); }); function follow() { const follow = { followee: getFirstUserId(), follower: currentUser } wixData.insert("following", follow) .then(updateValidity) .catch(console.log); } function getFirstUserId() { const username = $w("#username").label.toString(); return wixData.query("users") .limit(1) .eq("_id", username) .include("username", username) .find() .then(results => results.items[0]._id); } function updateValidity() { $w("#follow").label = "Unfollow"; }
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"; }