Why does member query result with an array of 0?

What is wrong about this code? I have been tried to use it on member pages and other pages. Results object returns 0 array.

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

$w.onReady(function () {
    let user = wixUsers.currentUser;
    let id = user.id
    wixData.query("Members/PrivateMembersData")
        .eq("_id", wixUsers.currentUser.id)
        .find()
        .then((results) => {
            console.log(results)
            if (results.items.length > 0) {
                name = results.items[0].name;
                $w("#investsDataset").setFilter(wixData.filter()
                    .eq("person", name)
                );
            }

        });

});

Note that the PrivateMembersData collection has Read permission for Site Member Author only.

You should do the query in the backend and use the SuppressAuth option set to true as demonstrated in the following code snippet:

import wixData from 'wix-data';

// ...

let options = {
    "suppressAuth": true,
    "suppressHooks": true
};

wixData.query("myCollection")
  .find(options)
  .then( (results) => {
    if(results.items.length > 0) {
      let items = results.items;
      let firstItem = items[0];
    } else {
      // handle case where no matching items found
    }    
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );

I have coded in the backend and solved the issue. It was just a part of code on that page, I have changed all of it after. Thank you for your reply.

1 Like