Trying to allow site members to create only one record

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)

You can set up a member profile to whatever you want it to be, just build your own and don’t use Wix Members app.
https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area
So if you wanted to take the site member to their own page, then it would simply be like this as shown in the tutorial above.

   wixLocation.to(`/Members/${wixUsers.currentUser.id}`);  

As you are using the Wix Members app, then every page which is within the Wix Members area will have a prefix of www.youwebsitename.org/account/pageURL
Setting up a Members Area | Help Center | Wix.com

Also, note that when you add the Wix Members app to your site, this collection will get added automatically to your site which the app uses.
Velo: Wix Members "PrivateMembersData" Collection Fields | Help Center | Wix.com

As for Wix Dataset fields, have a look here for the proper field names and field keys for them, ike owner ID which is _owner.
CMS (Formerly Content Manager): About Your Collection Fields | Help Center | Wix.com

Thank you!