Back

[Solved]Appwrite 1.4.2 / function / Get User returns error:{}

  • 0
  • Users
  • Functions
  • Self Hosted
Elie Waked
8 Sep, 2023, 20:16

as per the documentation the URL is optional whereas it is not

TL;DR
The user was experiencing an error with the Appwrite SDK version 1.4.2. They were not able to retrieve user information using the `Get User` function. They later realized that the response object from the function cannot be viewed in the function portal log. They also discovered that they were using an older version of the SDK (9.0.0) and needed to update it. They were also facing issues with the `createMembership` function and found that the URL parameter was not optional, contrary to the documentation. Their code was not waiting for the promise to resolve, so they were not getting any errors or response.
Elie Waked
8 Sep, 2023, 20:20

it is not working as well, the function is not waiting for the respone

TypeScript
  var promise = teams.createMembership(
    team_clientsID,
    [],
    'https://portal.xxxxxxxx.com',
    user_id
  );
  promise.then(
    function (response) {
      return res.json({
        Response: 'Success',
        userId: user_id,
        membership: response,
      });
    },
    function (err) {
      return res.json({
        status: 'Error',
        error: err.toString(),
      });
    }
  );
  return res.json({
    status: 'EOF',
  });
Elie Waked
8 Sep, 2023, 20:21

no errors no response from the promise

Elie Waked
8 Sep, 2023, 20:22
TypeScript
    * @param {string} teamId
     * @param {string[]} roles
     * @param {string} url
     * @param {string} email
     * @param {string} userId
     * @param {string} phone
     * @param {string} name
     * @throws {AppwriteException}
     * @returns {Promise}
     */
    createMembership(teamId: string, roles: string[], url: string, email?: string, userId?: string, phone?: string, name?: string): Promise<Models.Membership>;
   
Drake
8 Sep, 2023, 20:25

What version SDK?

Elie Waked
8 Sep, 2023, 20:25

"dependencies": { "node-appwrite": "^9.0.0" }, "devDependencies": { "prettier": "^3.0.0" }

Drake
8 Sep, 2023, 20:26

You're using an older SDK. That's why it's required.

Drake
8 Sep, 2023, 20:27

Based on this code, the return is called before the promise is resolved. That's why you don't see anything

Elie Waked
8 Sep, 2023, 20:27

in fact i made ``` appwrite init function```` using appwrite-cli

Drake
8 Sep, 2023, 20:28

You'll need to update dependencies if you want to use the newer SDK

Elie Waked
8 Sep, 2023, 20:28

i think it is a bug in the cli

Elie Waked
8 Sep, 2023, 20:28

anyways which sdk am i supposed to use

Drake
8 Sep, 2023, 20:29

Look at the package on NPM

Elie Waked
8 Sep, 2023, 20:30

ok

Elie Waked
8 Sep, 2023, 20:30

11.0.0

Elie Waked
8 Sep, 2023, 20:37

the same nothig new on users.get() nor on createMemebership()

Elie Waked
8 Sep, 2023, 20:38

will catch up tomorrow, thanks a lot @Steven

Drake
8 Sep, 2023, 21:18

please share your new code when you're back

Elie Waked
9 Sep, 2023, 04:36

Morning! Well i miss reading of the documentation led to the above cocktail. Indeed the res.json({}) is received by the client and can not be reveiled in the fuction portal log. as per the documentation

TypeScript
Response
If you need to send a response to the invoker of the function, such as a user, client app, or an integration, use the response object. The response information will not be logged to the Appwrite Console. There are several possible ways to send a response, explore them in the following Appwrite Function.
Elie Waked
9 Sep, 2023, 04:37

therefore the new code looks like that

TypeScript
import sdk from 'node-appwrite';

export default async ({ res, req, error, log }) => {
  const client = new sdk.Client();
  const teams = new sdk.Teams(client);
  const users = new sdk.Users(client);

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

  try {
    const user_id = req.headers['x-appwrite-user-id'];
    let user_email = '';
    const team_clientsID = process.env.TEAM_ACCOUNTS_ID || '';
    log(`Team ${team_clientsID} to join  ${user_id}`);
    log('Header ' + JSON.stringify(req.headers));
    const userGetResp = await users.get(user_id);
    user_email = userGetResp.email;
    log('User ' + JSON.stringify(userGetResp));
    var createMembershipResp = await teams.createMembership(
      team_clientsID,
      [],
      undefined,
      user_id
    );
    log('Create ' + JSON.stringify(createMembershipResp));

    return res.json({
      Response: 'Success',
      // userId: user_id,
      // email: user_email,
      createMembershipResp: createMembershipResp.toString(),
    });
  } catch (err) {
    error(err.toString());
    return res.json({
      status: 'Error',
      error: err.toString(),
    });
  }
};
Elie Waked
9 Sep, 2023, 04:38

the Log is as follow

TypeScript
Team clients to join  64fbf57e2b39116d4d4e
Header {"host":"localhost","user-agent":"Appwrite/1.4.2","x-appwrite-trigger":"event","x-appwrite-event":"users.64fbf57e2b39116d4d4e.create","x-appwrite-user-id":"64fbf57e2b39116d4d4e","content-type":"application/x-www-form-urlencoded","connection":"keep-alive","content-length":"406"}
User {"$id":"64fbf57e2b39116d4d4e","$createdAt":"2023-09-09T04:33:02.177+00:00","$updatedAt":"2023-09-09T04:33:03.012+00:00","name":"","password":"$argon2id$v=19$m=65536,t=4,p=3$SWYzdkdvd1RXTnVoZUVzSQ$AkHd3UwyqwoRvQoC3iTD8e4FzQ1vQlTj6BBOg+O+PaM","hash":"argon2","hashOptions":{"type":"argon2","memoryCost":2048,"timeCost":4,"threads":3},"registration":"2023-09-09T04:33:02.177+00:00","status":true,"labels":[],"passwordUpdate":"2023-09-09T04:33:02.177+00:00","email":"qwerwr23elie@eswplus.com","phone":"","emailVerification":false,"phoneVerification":false,"prefs":{"avatar_id":null,"referred_by_agent_id":"","auto_funding":false,"first_name":"","last_name":"","address":"","country":""},"accessedAt":"2023-09-09T04:33:02.177+00:00"}
Create {"$id":"64fbf57f9b46335da809","$createdAt":"2023-09-09T04:33:03.636+00:00","$updatedAt":"2023-09-09T04:33:03.636+00:00","userId":"64fbf57e2b39116d4d4e","userName":"","userEmail":"qwerwr23elie@eswplus.com","teamId":"clients","teamName":"clients","invited":"2023-09-09T04:33:03.636+00:00","joined":"2023-09-09T04:33:03.636+00:00","confirm":true,"roles":[]}
Elie Waked
9 Sep, 2023, 04:39

as per the new code i was able to receive the client information and create the new membership without the need of the email with just the userId

Elie Waked
9 Sep, 2023, 04:39

Thanks a lot @Steven

Elie Waked
9 Sep, 2023, 04:43

[Solved]Appwrite 1.4.2 / function / Get User returns error:{}

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