Querying if specific collection field is empty

Hello everyone, I am trying to use a single button to make 2 queries in 2 collections, if the conditions of both queries are positive the user can go forward, the 2nd query by itself works well (it is limited to if the user already has 5 items in the collection, a button appears that sends him/her to another page). But I have problems with the first query, it must look to see if the user already has their email in the “Brokers” collection, if not, a button appears that sends them to another area, but if so, the main button with which makes the query and sends it to another area.

First, I don’t know if two queries like these can be made with a single static on_click event, but if so, this code always gives me the action that the user does not have an email, even if the email is in the collection correctly. What am I missing?

I still can’t test the second query together with the first (it is currently all commented with //), since it is only the first one that is not working for me, if we manage to solve the email query, I would remove the comments // and I guess Everything should work fine, I hope for your help my dear friends.

import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

let user = wixUsers.currentUser.getEmail()
        .then((email) => {
            console.log(email);
        });

export function newInmoButton_click(event) {
   
    wixData.query("Brokers")
        .eq("email", user)
        .find()
        .then((results) => {
            if (results.items.length === 0) {

                $w('#newInmoButton').hide(),
                    $w('#warning').hide(),
                    $w('#planesButton').hide(),
                    $w('#fichaButton').show(),
                    $w('#warning2').show()

            } else {
                $w('#newInmoButton').show()
                wixLocation.to(`/corredoreslook/inmuebles-basico/nuevo/${wixUsers.currentUser.id}`)

            }
        })
// Second query placed as a comment starts here // // // // 
        wixData.query("Inmuebles")
          .eq("_owner", wixUsers.currentUser.id)
         .find()
        .then((results) => {
        //     if (results.items.length > 4) {
               $w('#newInmoButton').hide(),
                 $w('#warning').show(),
               $w('#planesButton').show(),
             $w('#fichaButton').hide(),
           $w('#warning2').hide()

             } else {
               $w('#newInmoButton').show()
             wixLocation.to(`/corredoreslook/inmuebles-basico/nuevo/${wixUsers.currentUser.id}`)
             // handle case where no matching items found
        }
        })
// Second query placed as a comment ends here // // // //
        .catch((err) => {
            console.log(err);
        });
}
export function fichaButton_click(event) {
	 wixLocation.to(`/corredoreslook/update/${wixUsers.currentUser.id}`)
}

This is the code that contains the issue. What’s happening here is that the email is being retrieved but not returned so that it can be assigned to user.

What you need to do is change it to return the email, as so:

let user = wixUsers.currentUser.getEmail()
        .then((email) => {
            console.log(email);
            return email;
        });

I’ve personally done this many times before and driven myself a bit crazy over it :slight_smile:

Thanks, I didnt know that, I really appreciate the help, but it didn’t work, although the email is in the collection, the behavior is as indicated as if there was no email, any other idea? or observation?

I’m going crazy, No matter what I do it’s like there is no email in the collection at all; I created a new page just for testing, I have opened the debug/console to see a little more, I am not very knowledgeable btw; If in the condition I tell that if there is at least one match, gives me this message which I assume is the “result” of if (results.items.length > 0) as if there were no email, even if there was.

Summary

“jsonPayload”:{“message”:“{”_executeQuery":“function(t){return e._pagingFunction.bind(t)()}”,“_withCalculatedSkip”:“function(t,r){return t.skipNumber=yt(e.query,e.length,r),Promise.resolve(t)}”,“_items”:,“_totalCount”:0,“_query”:{“invalidArguments”:,“filterTree”:{“$and”:[{“correo”:“Promise<>”}]},“encoder”:{},“provider”:{“cloudDataUrl”:“/_api/cloud-data”,“gridAppId”:“141bc1b4-6acc-404c-94e2-8c5bb3046563”,“segment”:“LIVE”,“httpClientProvider”:“function(){!function(){if(m()){var t=s?Date.now()-Math.round(s):null,r="site"===d?i.active$wSiteViewMode({isPopup:u,isServerSide:c,pageId:l,pageNumber:f,pageUrl:p,tsn:t}):i.active$wPreviewMode({pageNumber:f,pageUrl:p,tsn:t,pageId:l});e.bi(r),v=!0}}();for(var r=arguments.length,n=new Array(r),o=0;o<r;o++)n[o]=arguments[o];return t.apply(this,n)}”,“traceWith”:“function(){!function(){if(m()){var t=s?Date.now()-Math.round(s):null,r="site"===d?i.active$wSiteViewMode({isPopup:u,isServerSide:c,pageId:l,pageNumber:f,pageUrl:p,tsn:t}):i.active$wPreviewMode({pageNumber:f,pageUrl:p,tsn:t,pageId:l});e.bi(r),v=!0}}();for(var r=arguments.length,n=new Array(r),o=0;o<r;o++)n[o]=arguments[o];return t.apply(this,n)}”,“authHeader”:{“get”:“get(){return e.sessionService.getWixCodeInstance()}”}},“collectionName”:“Brokers”,“skipNumber”:0,“included”:,“projectedFields”:},“_partialIncludes”:false,“_pagingFunction”:“function(){return this.find(t)}”,“_hasNext”:false,“nextSkipNumber”:0,“prevSkipNumber”:0}"
}

But, if in the condition I say that if there is no match there if (results.items.length === 0), then the debug/console gives me this other message, as if there were no email, even if there was:

“jsonPayload”:{
“message”:
“No Email[object Object]”
}

I have no idea what that is, I just know that both responses in the console are different and that I am quite lost here. Please help :pleading_face:

this is the code that is being used

import wixData from 'wix-data';
import wixUsers from 'wix-users';

let user = wixUsers.currentUser.getEmail()
        .then((email) => {
            console.log(email); //the email with which the user registered, not the one he added to the collection
            return email;
        });

export function button10_click(event) {
    
    wixData.query("Brokers")
        .eq("email", user)
        .find()
        .then((results) => {
            if (results.items.length > 0) {  //if (results.items.length === 0)
                console.log(results);

            } else { 
                console.log("No Email" + results); 
                 }
        })
        .catch((err) => {
            console.log(err);
        });
}

Solved! Finally peace in my head. It has to be this way, but thanks anyway @anthony

import wixData from 'wix-data';
import wixUsers from 'wix-users';

let user = wixUsers.currentUser;
let userEmail;
user.getEmail()
    .then((email) => {
        console.log(email);
        userEmail = email;
    });

export function button10_click(event) {
    
    wixData.query("Brokers")
        .eq("email", userEmail) // userEmail instead of user
        .find()
        .then((results) => {
            if (results.items.length === 0) {  
                console.log("No Email" + results);

            } else { 
                console.log("Yes! You got it Baby!" + results); 
                 }
        })
        .catch((err) => {
            console.log(err);
        });
}


1 Like