QueryContacts returning 'Unable to handle the request'

Hello - I am very new to Velo coding and am trying to use the queryContacts functionality to pass a user’s email to my backend function, search for a corresponding field that is registered with that user’s email, and then pass this information back to the frontend in order to change a link based on the result.

I have put my code below - in the site logs, I am receiving the following error: ‘Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.’ The backend code seems to be working as it functions when I test it using the editor and provides the correct results based on my database.

I have looked at other posts about this but am not able to find an answer as to why the published page is not functioning (I looked into my database settings / am using SuppressAuth, etc.)…can someone please help me here?

My Frontend Code:

import { myUserType } from 'backend/userType.jsw';
let email;

$w.onReady(function () {
    //get current user email
        currentMember.getMember()
        .then((member) => {
            email = member.loginEmail;
            session.setItem("email", email);    
        })
        
    myUserType(email).then( product => {
         console.log(product);
         if (product == "userA") {
            console.log("user type: userA");
            $w("#button3").link = "/explore";
         } else {
            console.log("user type: userB");
            $w("#button3").link = "/contact";
         }
     })
 });

My Backend Code (userType.jsw):

import { contacts } from 'wix-crm-backend';

export async function myUserType( email ) {
    const results = await contacts.queryContacts()
    .eq('primaryInfo.email', email)
    .find({'suppressAuth': true});
    const userType = results.items[0].info.extendedFields["custom.user"];
    return userType;
}

Hi @lexi-datta ,

The error log on the frontend does not display the actual reason of failure (Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.)

Please learn more on how to monitor logs from the backend.

Does it solve your questions?

Certified Code

Hello - thanks for the prompt response. So I did actually look at this page and the site logs but it’s showing the same error message. I have put a screenshot of what I see below (I removed a few console.logs from the code above to clean it, but that is what is showing up in the site monitoring).

I may be missing something very obvious, but the only other strange thing I saw in the logs was that my backend logs / errors are showing up before the frontend getMember() logs. As I mentioned above, when I test just the backend piece in the editor, I am not receiving the ‘Cannot read property…’ errors and it functions correctly.

Could this be an issue with the timing of the functions?

@lexi-datta I looked at your code and I suspect it is happening in your backend code. The code is failing when trying to access info.

What I suspect is happening is your query is not returning any results. So you end up with an empty recordset being returned.

You are trying to use

const userType = results.items[0].info.extendedFields["custom.user"];

but you are not doing any validation to ensure you got records returned.
I’ve adjusted your backend code a bit, and put in some error checking. I also changed some of the ‘const’ declaration to ‘let’. I find that a lot of examples misuse ‘const’.

Here is the changed code, see if this helps. I have also included a few console.log() messages that should give you some help if something goes wrong.

import { contacts } from 'wix-crm-backend';

export async function myUserType( email ) {
    return contacts.queryContacts()
    .eq('primaryInfo.email', email)
    .find({'suppressAuth': true})
    .then( (results) =>{
        if (results.length >0) {
            let userType = results.items[0].info.extendedFields["custom.user"];
            return userType;
        } else {
            console.log("[myUserType]::No record was found when querying queryContacts.  Email was: "+email);
            return ""; // no record was returned so return blank usertype
        }
    })
    .catch( (error) => {
        console.log("[myUserType]::Error occured trying to run queryContacts.  Error was: "+ error.message);
        return ""; // no record was returned so return blank usertype
    });
}  
   

I changed your style a little bit. I saw this similar example using this style at:

I was not able to test to run this so hopefully I didn’t make a mistake in typing.

A very important thing to note is the backend query uses .eq().
That means the email must match exactly. The alphabetic case (upper/lower) has to match exactly and there can’t be any spaces etc. If it does not match exactly, your query will fail and you will not get back any results.

You could also try replacing the .eq() with

.contains('primaryInfo.email', email)

Using .contains(), is more forgiving in that it is a case insensitive search and will still find it if there are spaces after it.

Paul, thanks so much for the reply!

I replaced my code with what you suggested and it is no longer throwing any errors (for the .contains() suggestion, I looked at the Velo API reference and think you were referring to .hasSome, which I replaced .eq with) - however, I am now having the issue where the the backend code in the editor is returning the correct result but when I access the published site, it is not throwing an error but is also returning an incorrect (empty - see console logs below) result.

I have put a screenshot of both results below (have replaced ‘UserA’ above with ‘chef’ and ‘UserB’ with ‘user’ to make it easier to differentiate) - do you think this could be because of an issue with permissions? I thought that suppressAuth: true would solve that issue.

Thank you!

Backend Returns:

Published Code Returns:

The site logs are also recording an issue with the published site as the following:

Can you show your backend code and your front end code. I can’t follow what you are doing. I am thinking your calling your ’ myUserType’ in the backend. You can only use the suppress options when running queries in the backed. The options will not work if you try and run the query in the frontend. Having your frontend code call the backend to run the query is fine. But without seeing your code I can’t make any sense of what you are doing.

What I can confirm is that you contacts query is definitely failing. 9 out of 10 times it is usually permissions related, in that you do not have the correct permissions to run the query.

Sure - most of the code is the same as the original post, except for the part that I replaced with yours. The suppressAuth is in the backend (in the .find() function). Thank you!

Front-End Code:

import { authentication, currentMember } from 'wix-members';
import {local, session, memory} from 'wix-storage';
import { myUserType } from 'backend/userType.jsw';
let email;

$w.onReady(function () {
    //get current user email
        console.log("frontend");
        currentMember.getMember()
        .then((member) => {
            console.log("user is logged in")
            email = member.loginEmail;
            session.setItem("email", email);    
            console.log("got current member, email: ", email);
        })
        
    myUserType(email).then( product => {
         console.log(product);
         if (product == "chef") {
            console.log("user type: chef");
            $w("#button3").link = "/explore";
         } else {
            console.log("user type: user");
            $w("#button3").link = "/contact";
         }
     })
 });

Back-End Code:

import { contacts } from 'wix-crm-backend';

export async function myUserType( email ) {
    return contacts.queryContacts()
    .hasSome('primaryInfo.email', email)
    .find({'suppressAuth': true})
    .then( (results) =>{
        if (results.length > 0) {
            console.log(results);
            let userType = results.items[0].info.extendedFields["custom.user"];
            console.log(userType); 
            return userType;
        } else {
            console.log("[myUserType]::No record was found when querying queryContacts.  Email was: "+email);
            return ""; // no record was returned so return blank usertype
        }
    })
    .catch( (error) => {
        console.log("[myUserType]::Error occurred trying to run queryContacts.  Error was: "+ error.message);
        return ""; // no record was returned so return blank usertype
    });
}