Conditional - looking up database "owner" field

Hi!
Hope someone can help me.

I have the following situation:

User is logged in already.
User clicks on Button.
Button looks up dataset.
If there is a item on the dataset where the field _owner is equal to currentuser_id, then redirect to page 1.
If there is no row on database that matches the condition, redirect to page 2.

Code not working:

export function button1_onclick() {
wixData.query(“Restaurants-and-Clubs”)
.eq(“_id”, wixUsers.currentUser.id )
.find()
.then( (results) => {
if (results.items.length === 0)
{
wixLocation.to (/Dashboard/Restaurant Creation);
}
else
{
wixLocation.to (/Restaurants-and-Clubs/Update/${ [wixUsers.currentUser.id](http://wixusers.currentuser.id/) });
}
});
}

Many thanks in advance :slight_smile:

Hi Ivana,

How is it not working? That is, what is it doing that isn’t correct?

You can take a peek at what’s happening by adding console.log() statements in different places in the code. This will give you an opportunity to see how the code is behaving and what’s being returned from the query.

Yisrael

1 Like

Hi Yisrael!

Thanks for replying. Just found out my first mistake: _id instead of _owner on my query.

Now it’s working fine and identifying that I have an item already created, and my second problem is…

wixLocation.to (/Restaurants-and-Clubs/Update/${ [wixUsers.currentUser.id](http://wixusers.currentuser.id/) });

Because the ID of the item on this database (the one I am using for the SEO) is different from the User ID.

Is it possible to query then the row with the _owner I want, and get the item ID?

Thank you very much!

Hi Ivana,

Glad to hear that you worked it out. Now I can sit back with my beer and…

Second problem? That’s not fair. My beer will go flat. :upside_down_face:

You have the _owner value, now you can just do another query to get the item - however you want. You can query based on _owner or _id. Depends on what works. Really shouldn’t be a problem.

Ah - and my beer barely lost any fizz. And don’t worry - I’m still here if you need more help.

Yisrael

1 Like

Hi!

Hahaha, I will probably own you a pack of beer by the end of it.

Sooo… I tried to use the results from the query straight away:

export function button1_onclick() {
wixData.query(“Restaurants-and-Clubs”)
.eq(“_owner”, wixUsers.currentUser.id)
.find()
.then( (results) => {

     if (results.items.length === 0) { 
        wixLocation.to(`/Dashboard/Restaurant Creation`); } 
     else 
     { 
    let item = results.items; 
	let itemid = item._id; 
     wixLocation.to(`/Restaurants-and-Clubs/Update/${itemid}`); 
      } 
 }); 

}

But now I can getting “undefined” as result for my itemid. :frowning:

And also… somehow the first query is starting to behave a bit weird. It went to the wrong page a couple of times.

Thaank you!!

Hi again,

Not sure what you mean by “the first query”. I only see one.

The results are returned as an array. So, you’ll need to fix the code a little bit:

if (results.items.length === 0) {
    wixLocation.to(`/Dashboard/Restaurant Creation`);
}
else {
    let items = results.items;    // items is an array of results
    let item = items[0];     // you know there's only one - so get it
    let itemid = item._id;  // now get the field you want
    wixLocation.to(`/Restaurants-and-Clubs/Update/${itemid}`);
}

In the code above, I changed the way that the item is retrieved. I get the “first” (and what should be the only) item in the array. Then I get the _id field from that item.

I hope this does the trick.

Yisrael

Thank you veeeery much, you are a hero!

It’s working now. :slight_smile:

:beers:Yay!

hello, I want a similar function, but I want it on the “on_read” function and I want to see the user who is in the field name in the “owner” database to display the button

Hey Lukáš,

Owner is a hidden field in the database which has the field key of _owner . You can use this field in a database query and then compare it with the currentUser property of wix-users .

Good luck,

Yisrael

1 Like

Hi Yisrael,

I reused the code you added below and it works for the else function, but does not work for my if function. So when the user is not found I want to link to a dynamic page and somehow this function does not work.

Do you know what I´m missing?
Here is the Code:
import wixData from ‘wix-data’ ;
import wixUsers from ‘wix-users’ ;
import wixLocation from ‘wix-location’ ;

export function PunkteKonto_click ( event ) {

wixData . query ( “Memberliste” )

      . eq ( "_owner" ,  wixUsers.currentUser.id ) 

      . find () 

      . then ( ( results ) => { 

                

     **if**  ( results.items.length  ===  0 ) { 
wixLocation . to ( `/kontoaktivierung/{Name}` );  // does not link to dynamic page 

}
else {
let items = results.items ; /
let item = items [ 0 ];
let itemid = item._id ;
wixLocation . to ( /punktekonto/ ${ itemid } );

      } 

 }); 

}