Wix-data query returning nothing + a uncaught (in promise) error

This is my first time attempting to use wix or corvid properly and I’ve pretty quickly ran into a big issue. Whenever I attempt to query my PrivateMembersData database I never get anything back, it used to return nothing since when I used console.log my result would just say ‘undefined’ however I tweaked a few things since then after realising I should be putting return before making the query etc and even though what I have put now seems like its more correct I also now have an uncaught error.

The current situation is that I still don’t think I’m getting a valid response from the database since everything past my ‘.then(result => {’ doesn’t seem to run at all, along with some errors in the console saying ‘uncaught (in promise)’ with the next line in the trace being ‘convertToErrorObject’ but then it all seems to become quite meaningless after that. When I click on the convertToErrorObject part I get this message: ‘TypeError: Cannot read property ‘requestContent’ of undefined’.

So obviously I’ve done something wrong but saying I’ve never really tried making a website before I’m quite lost as it stands. I’ll attach my code below:

Page:

import wixData from 'wix-data';
import wixUsers from 'wix-users';
import {searchForUser} from 'backend/checkAccounts'
// For full API documentation, including code examples, visit https://wix.to/94BuAAs

var lettersNumbers = /^[0-9a-zA-Z]+$/;

$w.onReady(function () {
 // TODO: write your page related code here...

});

export function signUpClick(event) {
 // Add your code for this event here:
 
    $w("#user").onCustomValidation( (value, reject) => {
        console.log(value);
        searchForUser(value)
        .then(result => {
            console.log("Value ", result);
 if (result === "FOUND") {
                console.log("User exists");
                reject("Invalid username.");
                $w("#errorText").text = "Username already exists.";
            }else {
                console.log("User doesn't exist");
 if (!value.match(lettersNumbers)) {
                    reject("Invalid username.");
                    $w("#errorText").text = "Username must only contain letters and numbers.";
                }
            }
        });
    });
}

The backend ‘checkAccounts’ web module:

import wixData from 'wix-data';
// Filename: backend/checkAccounts.jsw (web modules need to have a .jsw extension)

export function searchForUser(user) {
 return wixData.query("Members/PrivateMembersData")
        .eq("name", user)
        .find()
        .then( (results) => {
 if (results.items.length > 0) {
 return "FOUND";
            }else {
 return "NORESULTS";
            }
        });
}

If anyone knows what I’ve missed any help would be greatly appreciated.

1 Like

I haven’t read the whole question but try:

 return wixData.query("Members/PrivateMembersData")
 //.....
 .find({suppressAuth: true})

Wow that was an easy fix! Why do I have to put suppressAuth though? Is this an unsafe way of handling queries?

@jacobhothersall only admins can run queries regarding other users’ private data. The permission for this collection is quite strict and you can’t change it.
However, for queries made from the back-end only, you can suppress the restrictions.

@jonatandor35 Okay that’s fantastic, thanks a lot.

You’re welcome. happy to help :slight_smile: