Back

[SOLVED] Update MembershipRoles of a specific user

  • 0
  • Self Hosted
  • Web
loup
25 Jun, 2023, 07:46

I try to update the membershiproles of an user when users.*.verification.*.update is triggered. Idk why my function return the error Error adding user to the role maybe there is something wrong with the query when listMemberships is used :

TypeScript
module.exports = async function (req, res) {
  const client = new sdk.Client();
  const teams = new sdk.Teams(client);
  const users  = new sdk.Users(client);

  if (
    !req.variables['APPWRITE_FUNCTION_ENDPOINT'] ||
    !req.variables['APPWRITE_FUNCTION_API_KEY']
  ) {
    console.warn("Environment variables are not set. Function cannot use Appwrite SDK.");
  } else {
    client
      .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
      .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
      .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
      .setSelfSigned(true);
  }

  const teamId = req.variables['APPWRITE_FUNCTION_TEAM_ID'];
  const token = JSON.parse(req.variables['APPWRITE_FUNCTION_EVENT_DATA']);
  const user  = await users.get(token.userId);
  if (user.emailVerification) {
    try {
      const members = teams.listMemberships(teamId, [
        Query.equal('userId', user.$id)
      ]);
      if (members.total === 1) {
        await teams.updateMembershipRoles(teamId, members.memberships[0].$id, ['verified'], 'https://appwrite.nvlab.fr');
        res.json({ success: `User ${user.name} added to the Verified Role successfully` });
      } else {
        throw 'Error looking for a membership user.';
      }
    } catch (error) {
      res.json({ error: 'Error adding user to the role' });
    }
  }
};
TL;DR
The user is facing an issue with updating the membership roles of a specific user. They are not sure why the query is not working and there is also an issue with the teamId. The error they are getting is "Error adding user to the role". Solution: 1. Make sure to await the `teams.listMemberships(teamId)` function call. 2. Check the value of the teamId variable and ensure it is correct. 3. Log the error and share it for further analysis. 4. Make sure to use the `await` keyword when calling `teams.listMemberships(teamId, [Query.equal('userId',
loup
25 Jun, 2023, 08:05

Okay the problem comes from my teamId I guess because even if i do :

TypeScript
console.log(teams.listMemberships(teamId);

No output

Drake
25 Jun, 2023, 08:06

It would help if you logged the error and shared it

Drake
25 Jun, 2023, 08:08

I think you need to await it

loup
25 Jun, 2023, 08:10

like that : await teams.listMemberships(teamId); ?

loup
25 Jun, 2023, 08:12

When I do console.log(error), ive got an empty {}

loup
25 Jun, 2023, 08:14

Well when i do that :

TypeScript
const membersOld = await teams.listMemberships(teamId);
      console.log('Members old', membersOld)
      const members = await teams.listMemberships(teamId, [
        Query.equal('userId', user.$id)
      ]);

membersOLD works but not members

loup
25 Jun, 2023, 08:25

well ive no idea why the Query.equal('userId', user.$id) dont work

loup
25 Jun, 2023, 08:25

There is no attribut for Team btw ?

loup
25 Jun, 2023, 10:15

ive no idea why the query dont works

Drake
25 Jun, 2023, 14:27

What's the error?

loup
25 Jun, 2023, 19:03

[SOLVED] Update MembershipRoles of a specific user

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more