Hide/show an element if a database reference field is empty

Hello, I need your help, guys ! :slight_smile:

Simple : I want to hide/show text elements if a specific multiple items reference field is empty in the database.

I tried a lot of variants but nothing is working for me at the moment.
Thanks for your help and for your time !

Hey there!

Did you not get your question answered in your other post?

If not, let me know what is different about this request.

Hello Robert !

No, actually, the code is not working with a reference type field, just with a classic text field. So, any help is welcome ! :slight_smile:

Query your collection using include(). [and include the reference field].
The if the retrieved object doesn’t contain this field, hide() the element.
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#include

@jonatandor35 Actually I don’t want it to include a specific object, only check if it is empty or not. So, I guess I cannot use incluse(), am I wrong ?

I tried this, not working either :

$w.onReady(function () {
wixData.query("MemberProfile")
  .include("livres")
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
  	$w("#message1").show();
  }
  else {
   $w("#message1").hide();
  }
} 
)}


Try:

if(results.items.length > 0) {
    results.items[0].livres ? $w("#message1").show() : $w("#message1").hide();
}

Not working…
And as often, when I add a block of code which is not working like this one, all the correct code of my page stop to be executed. I need to remove the failed block of code and everything re-exe correctly. Normal ?

Yes. Errors can stop the entire code. Please post your current code (together with my code).

@jonatandor35 Here it is :

$w.onReady(function () {
wixData.query("MemberProfile")
   .include("livres")
   .find()
   .then( (results) => {
 if(results.items.length > 0) {
         results.items[0].livres ? $w("#message1").show() :       $w("#message1").show();
         }
 else {
  $w("#message1").hide();
  }
  } 
  )}

@angelinesirba I can’t tell. but aren’t you missing something in your query?
shouldn’t it be something like:

//after you retrieve the userId
wixData.query("MemberProfile")
   .eq("userIdFieldKey", userId)
   .include("livres")
   .find()
   .then( (results) => {

I just tried to add the line and still not working. Quite deresperate !

add:
console.log(results.items);
and see if you see the “livres” data and post the “livers” logs here.

  • if you’re talking about items in a repeater (you never mentioned that) the code should be different)

I display items “livres” in a repeater, but the initial items are not stored in a reapeater but in the database “livres”.

So I have items (books) in my “livres” database + I have a reference fields in my “MermberProfile” database pointed to “livres”. And to each current logged in member, I want to display a text message if the reference field “livres” is empty in the “MemberProfile” database.

Where should I paste the console.log exactly in the code ?

And when I add .eq(“_id”, userId), userId is not defined.

At the begging of the code:

import wixUsers from 'wix-users';
let userId = wixUsers.currentUser.id;
//the rest of the code
  • make sure that the _id in your MemberProfile ollection is the same as in the Members/PrivateMembersData collection.

The ID alpha-numeric code is the same in MemberProfile and in Members/PrivateMembersData collection (example : 93cf4f5d-a961-4b38-961d-f973b11afbd7) but the name of this ID is different :

‘_id’ in MemberProfile
‘contactId’ in Members/PrivateMembersData collection

So here is my code (import wixUsers from ‘wix-users’; is at the top of the page code) and still, it doesn’t work :frowning:

Dear @yisrael-wix , any help for us ? :slight_smile: