Single Collection ID to update multiple collections?

Is there a way to allow a member - once logged in - to update a separate collection using their log in ID? i.e. Profile collection and product collection? They would need to be able to update (read/write) both collections once logged in…I’m good on permission understanding.

I also get that the login ID is per collection item and I’m trying to get around that since the signed in member will have multiple items that we want only the member to be able to update. There are a ton of details per item so I do not want to simply add more fields into the “profile” collection…

I know I am missing a query or create/insert in the below code, but this is what i have so far…


Button5 = Login - works
Button7 = Profile- works - connects to " Profile Collection"
Button8 = Offers - broken w “404” code - trying to connect to " Product Collection"

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w(“# button5 “).label = “Logout”;
$w(”# button7 “).show();
$w(”# button8 “).show();
}
else {
$w(”# button5 “).label = “Login”;
$w(”# button7 “).hide();
$w(”# button8 “).hide();
}
} );
export function button5_onclick() {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(”#button5”).label = “Login”;
$w(“#button7”).hide();
$w(“#button8”).hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;

// prompt the user to log in  
wixUsers.promptLogin( {"mode": "login"} ) 
  .then( (user) => { 
    userId = user.id; 
    return user.getEmail(); 
  } ) 
  .then( (email) => { 
    // check if there is an item for the user in the collection 
    userEmail = email; 
    return wixData.query(" **Profile** ") 
      .eq("_id", userId) 
      .find(); 
  } ) 
  .then( (results) => { 
    // if an item for the user is not found 
    if (results.items.length === 0) { 
      // create an item 
      const toInsert = { 
        " **_id"** : userId, 
        " **email** ": userEmail 
      }; 
      // add the item to the collection 
      wixData.insert( **"Profile** ", toInsert) 
        .catch( (err) => { 
          console.log(err); 
        } ); 

    } 
    // update buttons accordingly 
    $w("# **button5** ").label = "Logout"; 
    $w("# **button7** ").show(); 
    $w("# **button8** ").show(); 
  } ) 
  .catch( (err) => { 
    console.log(err); 
  } ); 

}
}
export function button7_onclick(event) {
wixLocation.to(/ **Profile** /${wixUsers.currentUser.id});
}
export function button8_onclick(event) {
wixLocation.to(/newdynamicpage/product/${wixUsers.currentUser.id});
}

Hey Ack,

That’s a really good question you have. If I understand correctly, you have a Profiles collection and a Products collection. Each record in “Products” has the userId of it’s owner, and you’d like to to allow each member to update his/her profile, and allow them to edit products, but only if they are the owner of that product. In addition, you want to to create a dynamic page that shows all the items of a specific user.

If this is the case, let’s talk about what you need to do to make this happen.

Before we start, I’d like to mention that instead of managing the login/logout button yourself, you can add a member login button by adding the “Member Login App” in the App Market. See more details here (the button will either appear under “More” in the add panel as the video shows, or it will be from the App Market under the name “Member Login App”).

For completeness, I’ll state that each record has a hidden System field called Owner . The userId of whoever created the record will appear there. Because of this, you don’t need to specify the userId when creating a new product or even a new profile.

Now, if you set up your Products and Profiles Collections to “Site Member Author” , only the user the created them will be able to edit them (and Admins as well)

As for the dynamic page you want to create, notice that what makes each page unique is the user, so what you should do is create another dynamic page on the Profiles Collection, but call this page “All Products of A Specific User” or something more appropriate. Set it’s URL to “/productsByUser/{ID}”.

On this page, you can get the data from Products Collection and filter it by the “_owner” field. You can do this by adding a dataset, connect it to the Products Collection, and using $w.onReady set the filter of the dataset :

$w.onReady(function () {  
  $w('#dataset1').setFilter(
    wixData.filter().eq("_owner", wixUsers.currentUser.id)
  )  
})

In the near future you’ll be able to filter by owner directly in the Dataset Settings Panel.

I hope this was helpful, let me know if you got to work and if you have any more questions :slight_smile:

J.

Hi Jonathan,

I also need the same function of Mr. Ack but mine is little bit different, I want to add another data under the same current owner . I’m currently working on a service dog registry, If the customer already put his/her service dog and he/she want to put another service dog under the same owner.

Hey Geo,

I’d be more than happy to put in my 2 cents. Please start a new post with a description of what you’re trying to do, what you already did (with code snippets and screenshots if you have them) and explain what didn’t work.

If you haven’t encountered a problem, and just don’t know where to start, I recommend taking a look at our tutorials and examples pages.

J.

Hi Jonathan, do you happen to know when the near future will arrive?

In the near future you’ll be able to filter by owner directly in the Dataset Settings Panel . <<< as stated by you? Seems quite a handy tool to use.

Kind regards,

Quyen Nguyen

Thanh Quyen Nguyen Phuong, have you figured out another way to do this by chance? Or perhaps you understood Jonathans explanation better than I did and it worked for you? let me know please and thanks

Hi ack,

Thanks for the reminding.

I have tried the first suggestion from Jonathan, so I created two dynamic pages one for the “profile” collection and the other for the “product” collection and in one the dynamic pages I applied the code, but it seems not to work.

$w.onReady(function () { $w(‘#dataset1’).setFilter( wixData.filter().eq(“_owner”, wixUsers.currentUser.id) ) })

I guess I misunderstood the dataset, I used the collection name. If you put the name of your dateset “#dynamicdataset” in the code, but except the field “_owner” I am a bit lost… I will try again later.

Then I tried to apply “reference” and “filter” in the dataset by linking the datasets with the same information, but this is only possible as read only. We want users to able to update in both colllections linking with the ID, but WIX has a hidden system “owner ID”. This seems not to be functional yet. If we could link the datasets with reference / filter tools with “Owner ID” , I think it will work without using wix-code. Correct me if I am wrong Jonathan? This is how I understood from your explanations and my experience with WIX. I just started out since the begining of this year. I still need to learn and understand WIX’s possibilities and capabilities.

Thanks in advance if you are able to elaborate more information/instructions for us.

Kind regards,

Quyen Nguyen

Hey Jonathan

What if I want the visitor to view each of the owner product page?