[BUG] Query to User table returns only logged in user

Hi, I need to query to “User” table. The strange thing here is that:

  1. If I am not logged in, I retrieve empty array of users from “User” table. Whereas I should have 2 users now in my “User” table.
    image

  2. If I am logged in, I retrieve array of my logged-in user only. Whereas I should retrieve the 2 users both from my “User” table.
    image

This is my code, very simple. I just comment out the login/logout line to try the case 1 and 2 above.

import React from "react";
import Moralis from "moralis/dist/moralis.min.js";
import { useEffect } from "react";

export const TryUser = () => {
  useEffect(async () => {
    // login
    //Moralis.User.logIn("[email protected]", "1234");

    // logout
    Moralis.User.logOut();

    const user = new Moralis.Query("User");
    const userResults = await user.find();
    console.log(userResults);
  });

  return <div></div>;
};

And this is my “User” table

If I do query like
user.equalTo("username", "user2");

  • The result will be empty array if I am logged in as user1.
  • Meanwhile, the result is received if I am logged in as user2.
1 Like

you can now query User table for all the users without using master key, that is because there is an ACL for every user row

Thanks for your reply. Sorry could you explain what should I do to query user table?

I’m trying to understand your answer above by googling haha, ACL is Access Control List. Which means that User table has ACL and causing it limited to be queried.

Master Key, from what I search in Moralis documentation, we need to use Master Key in the query
const results = query.find({ useMasterKey: true })

However, from your sentence. you say that

you can now query User table for all the users without using master key,

But why my query doesn’t work?

I’m checking the version https://github.com/MoralisWeb3/Moralis-JS-SDK the latest version as per today 2 Jan 2021 is v0.0.178 which was released 3 days ago.

While my Moralis in package.json runs in
"moralis": "^0.0.176",

Do you mean I need to update the moralis version to fix the query?

Else, I need to use the useMasterKey: true when querying User table?

I wanted to write not instead of now there.

You can only make that query from a cloud function using master key, you can not make if from front end.

Oh… thanks for the explanation. I will try to use cloud function and post after I can successfully do it.

Please update the docs, because in the docs it seems that we can query User table directly.
https://docs.moralis.io/moralis-server/database/queries

Another question, do I have to run it in Moralis Cloud function? Or can I run my own backend nodejs?

it works on your backend nodejs too, you’ll have to provide master key on Moralis.start

1 Like

Okay thanks @cryptokid.
Now I’m having problem using the cloud function, I post it in another thread, I’d really appreciate help.