Allow site members to edit their existing data

I am trying to create a form that members can add info to their existing data fields in a collection. When a member signs up, I want them to be able to update their profile information that is stored in a database. I want to add another collection of data that has a list of products that they carry. How do I reference their ID from the User Profile Database to the Products database, so they can add products to their own Profile. I do not know how to write the code to reference their UserId from the backend to add to the Product Database. Please help. I’ve been trying to figure this out for over a week. Any assistance would be fantastic!

Please see my screenshot of the form I created below.

Greetings,

In your products collection, if you haven’t done so already, you need to add a hook , so that you can insert the ID from the Wix-generated PrivateMembersData collection into your custom products collection.

In the code page that comes up, the following code would accomplish that task. This presupposes that you have already added a field in the Products collection (or whatever you actually call it) called “userId”.

import wixUsers from 'wix-users-backend';

export function Products_beforeInsert(item, context) {
    let MemberId = wixUsers.currentUser.id;
    item.userId = MemberId;
    return item;
}

Having done this, you now have linkage between the member and their products because every Product collection record would have the corresponding userId of the member. You can then filter and query accordingly.

Thank you for getting back to me, Anthony. Is the userId that same as the field that I called “owner” when I first created the database?

Also, should the userId be a “reference” or “text” field?

@johannabgaber I’m wondering how you got the “owner” field populated with an ID? It looks like the same ID for every one of the records showing in the screenshot. Does that mean it’s the same user for all of those products?

@tony-brunsman Hi.
Yes we manually entered the info because the site has not “debuted” yet. When is does, we want new members to enter their own data.

@tony-brunsman
Another question I have is how do I link the “login” page to my PrivateMembersData? I should let you know that we have two types of members: Vets & Vendors . They will be differentiated by CRM “labels”, once they are approved. Therefore, I want to create a new field in the PrivateMemberDatabase that shows the user’s assigned label (ie. Vets or Vendors). Since the permissions won’t all me to edit the PrivateMemberData, how do I go about doing this? The only reason I need to know this is because, depending on which label a member has, they will be directed to either a Vet Info Form or a Vendor Info Form (like the one I attached to my first question above.

So basically I need to know how to direct users to the correct form according to their label after they log in. I also don’t know how to filter and query the corresponding data from allMembers to the either the Products Database or Vets database. I’m sorry, if this is confusing. Please let me know if I can provide more info.

Thank you so much, again!
Below I have pasted my current “login” page with the code, as well as the VendorProfile Update Page with the code, that was in my first post.


import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;

$w.onReady( function () {
$w(‘#login’).onClick( function () {
let email = $w(‘#email’).value;
let password = $w(‘#password’).value;
wixUsers.login(email, password)
.then(() => {

            console.log("User is logged in");$w('#wrongLogin').hide(); 
 wixLocation.to('https://www.therapaw.com'); 

} )
. catch ( (err) => {
console.log(“Wrong username or password”);$w(‘#wrongLogin’).show()
} )
})
})
let isUserLoggedIn = wixUsers.loggedIn; // true
//Add your code for this event here:

export function forgotPassword_click(event) {
wixUsers.promptForgotPassword();
//Add your code for this event here:
}
wixLocation.to(‘https://www.vitalvet.org/allMembers/VendorProfileUpdate/{ID}’)

I’m sorry to keep asking the same question, but can you explain where I need to add this code:

import wixUsers from ‘wix-users-backend’;

export function Products_beforeInsert(item, context) {
let MemberId = wixUsers.currentUser.id ;
item.userId = MemberId;
return item;
}
I want to clarify what I was asking in my last question. Is it possible to create a hook for the PrivateMemberDatabase that recognizes the user’s membership “role”? My site has two types of membership roles: one for “Vets” and one for “Vendors”. Each role has different page access permissions. The data for Vendors is stored in the “Products Database” and the data for Vets is stored in the “Facilities Database”. So, when they’re logged in, I want them to be able to update their existing info that is stored in either the “Facilites” or “Products” databases, depending on their assigned role. How do I connect both the Product and Facilities databases to the UserID in the PrivateMemberData. Do I need to create hooks and filters and how? I apologize, as I am not very experienced with writing code.
Thank you!