Back

[solved] receive context.res.send from client

  • 0
  • Flutter
  • Functions
sharpsaravanan
9 Oct, 2023, 06:48

how to receive the string in the client. the cloud function returns as "return context.res.send("Hello, World!");"

TL;DR
The user had an issue with receiving a message through `context.res.send` from the client. The problem was that they were returning the message from the catch block instead of outside the try/catch block. The solution was to change the return statement to be outside the try/catch block. Additionally, the user wanted to receive an error message in the client. They were advised to return `error.message` in the catch block.
sharpsaravanan
9 Oct, 2023, 06:55

unable to retrieve the error code from the cloud function captured during the execution inside the cloud server.

darShan
9 Oct, 2023, 07:01

You need to return the error to the client? Have you tried: return context.res.send(error.message)?

sharpsaravanan
9 Oct, 2023, 07:15

yes

sharpsaravanan
9 Oct, 2023, 07:17

final execution = await functions.createExecution(functionId: '65211abe4aade6923e7c', body: jsontestnew);

sharpsaravanan
9 Oct, 2023, 07:21

this funtion creates new user

sharpsaravanan
9 Oct, 2023, 07:23

function is working fine

sharpsaravanan
9 Oct, 2023, 07:24

i want to receive the error.message in the client

sharpsaravanan
9 Oct, 2023, 07:26

this is the exception object string when error occurs.

sharpsaravanan
9 Oct, 2023, 07:26

{$id: 65219a57e8f7da6b8c4f, $createdAt: 2023-10-07T17:50:19.578+00:00, $updatedAt: 2023-10-07T17:50:19.578+00:00, $permissions: [read("user:64d6987a54e50a844777")], functionId: 65211abe4aade6923e7c, trigger: http, status: failed, requestMethod: POST, requestPath: /, requestHeaders: [], responseStatusCode: 500, responseBody: , responseHeaders: [{name: x-powered-by, value: Dart with package:shelf}, {name: date, value: Sat, 07 Oct 2023 17:50:19 GMT}, {name: content-length, value: 0}, {name: x-frame-options, value: SAMEORIGIN}, {name: content-type, value: text/plain; charset=utf-8}, {name: x-xss-protection, value: 1; mode=block}, {name: x-content-type-options, value: nosniff}], logs: , errors: , duration: 2.8738858699798584}

sharpsaravanan
9 Oct, 2023, 07:28

i want this message "AppwriteException: user_already_exists, A user with the same id, email, or phone already exists in this project. (409)". to be received from client

darShan
9 Oct, 2023, 07:29

can you post your function method, you just need to propogate the message properly to client.

darShan
9 Oct, 2023, 07:32

a general idea like this can be used -

TypeScript
export default async function (context) {
  let message;
  try {
    await createUser();
    // no exception, it worked
    message = "success"
  } catch(error) {
    message = error.message;
  }

  return context.res.send(message);
}
darShan
9 Oct, 2023, 07:34

Client side: let message = executionStatus?.responseBody.

sharpsaravanan
9 Oct, 2023, 07:36

responseBody is empty

sharpsaravanan
9 Oct, 2023, 07:37

when user creation failed responseBody is empty

sharpsaravanan
9 Oct, 2023, 07:38

on sucess responseBody is user object

sharpsaravanan
9 Oct, 2023, 07:47

Future result = users.create( userId: data.userid!, //'[USER_ID]', email: data.email!, //'email@example.com', password: data.passw!, //'password', phone: data.phone, //'phone', name: data.name, //'phone', );

TypeScript
User mm = await result;
return context.res.send(mm.toMap().toString());

} catch (e) { //print(e.toString()); context.error("Failed to create user " + e.toString());

TypeScript
return context.res.send('error:' + e.toString());

}

sharpsaravanan
9 Oct, 2023, 07:49

this is the error log

sharpsaravanan
9 Oct, 2023, 07:49

Failed to create user AppwriteException: user_already_exists, A user with the same id, email, or phone already exists in this project. (409) Execution timed out.

sharpsaravanan
9 Oct, 2023, 08:23

i returned the context.res.send from the catch block. that's the problem. Now i changed to return from --outside the try/catch block. function works correctly.

sharpsaravanan
9 Oct, 2023, 08:29

received the message through executionStatus?.responseBody.

sharpsaravanan
9 Oct, 2023, 08:29

😀 thank you

sharpsaravanan
10 Oct, 2023, 04:36

[solved] receive context.res.send from client

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