Hello,
I’m trying to set up a site wherein members can establish profiles that potential employers can search through using a wide variety of factors and variables (such as years of experience, proficiency in certain skills, etc). The setup of the dataset and permissions is easy enough and does not require Corvid.
However, I don’t want members to be able to create more than one record per member… And since it doesn’t seem possible to add fields to the member profile itself, I’m setting it up as a separate option for logged-in members.
What I had tried to do here was use a data query to determine whether the member ID already existed in the new “Directory” collection as an “ownerId” field… if it does already exist, the script forwards to a dynamic page keyed to the ownerId. If the record does not exist, the script obtains the member ID value and creates a new record in the “Directory” collection with that value as an “ownerId” field before forwarding to the dynamic page.
The second part of the script works fine on its own (adding the record if it does not already exist), but once I add it to an if/else situation, it no longer functions.
Here’s what I’ve got so far:
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
let user = wixUsers.currentUser;
let ownerId = user.id;
$w.onReady( function () {
$w(“#directory”).onReady( function () {
wixData.query(“Directory”)
.find(“ownerId”, user.id)
.count()
.then( (num) => {
var numberOfItems = num;
})
})
})
export function dirbutton_click(event, $w)
{
if (numberOfItems === 0)
{
let toInsert = {
“ownerId”: user.id
};
wixData.insert(“Directory”, toInsert)
.then( (results) => {
let item = results; //see item below
} )
. catch ( (err) => {
let errorMsg = err;
} );
wixLocation.to(“https://www.policybear.org/directory-entry/” + ownerId)
// circumstances where record does not already exist
} else {wixLocation.to(“https://www.policybear.org/directory-entry/” + ownerId)}
}
There is a error message around the test on the “numberOfItems” variable, saying it’s not defined, despite my attempt to set it globally above. I have also attempted to set that variable using “session.setitem,” but it does not work. (I have also tried to group these elements into the same scope by putting it all on an intermediary page, but that doesn’t seem to have worked.)
What have I been missing? (URL: https://www.policybear.org/account/my-account)