mongodb-queryparse-server

Get User Role in Parse


I'm trying to get a user roles on login with in a cloud function. I know its a very simple task, but I am unable to solve it. I tried the way docs have mentioned using innerQuery that doesn't seem to work either. Since I'm relatively new, I am sure I'm making a silly mistake. Can you please point out my mistake?

My function looks like this:

Parse.Cloud.define('login', async (req, res) => {
    // TODO: put a validation layer here
    // if (!req.user) res.error('user undefined');

    if (!req.params.email || !req.params.password) res.error('email/password is required');
    const userQuery = new Parse.Query(Parse.User);
    const roleQuery = new Parse.Query(Parse.Role);
    try {
        const user = await Parse.User.logIn(req.params.email, req.params.password);
        const userRoleQuery = user.relation(Parse.Role).query();

        const role = await userRoleQuery.find();

        res.success(role);
    } catch (error) {
        res.error(error);
    }
});

attached is my _Join:users_Role table as well: I am querying on the second user.

image


Solution

  • You also can do it like:

    Parse.Cloud.define('getRole', async (request) => {
      const query = await new Parse.Query(Parse.Role).equalTo('users', request.user).find()
      return query
    })
    

    Or you can use it in your client just with the second row