Back

Weird 500 Error while Getting User

  • 0
  • Self Hosted
  • Functions
ZachHandley
23 Feb, 2024, 19:03

Hey so I have a 2FA function that runs and is giving me an error while retrieving my User ID. I've confirmed it exists, and the error is just kinda weird

TypeScript
MFA Setup -- User ID: 659479ac0d6bbe8e70d1
Getting user
{"code":400,"type":"","response":"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n         \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n <head>\n  <title>400 Bad Request</title>\n </head>\n <body>\n  <h1>400 Bad Request</h1>\n </body>\n</html>\n"}

----------------------------------------------------------------------------
Unsupported logs detected. Use context.log() or context.error() for logging.
----------------------------------------------------------------------------
(node:22) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:22) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.

----------------------------------------------------------------------------

any advice is appreciated

TL;DR
Developers are experiencing a weird 500 error when trying to retrieve a user ID for a 2FA function. The error message appears to be related to a bad request response. The catch block is logging the error, and logs show the error occurring during the process of getting the user. The suggestion is to change the logging method to `context.log()` or `context.error()` and check if the fetch operation within the `getCurrentDateTime()` function is working as expected. Solution: 1. Change logging method to `context.log()` or `context.error()`. 2. Confirm the `getCurrentDateTime()` function's fetch operation is functioning correctly.
ZachHandley
23 Feb, 2024, 19:04
TypeScript
async function getCurrentDateTime() {
  try {
      const response = await fetch("http://worldtimeapi.org/api/timezone/America/New_York");
      if (!response.ok) {
          throw new Error('Network response was not ok');
      }
      const data = await response.json();
      return data.datetime;
  } catch (error) {
      console.error('There has been a problem with your fetch operation:', error);
  }
}

// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }) => {
  // Why not try the Appwrite SDK?
  //
  const client = new Client()
    .setEndpoint('https://appwrite.pva.ai/v1')
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setKey(process.env.APPWRITE_API_KEY)
    .setSelfSigned(true);
  const users = new Users(client);
  const db = new Databases(client);
  const timestamp = req.body.timestamp;

  // The `req` object contains the request data
  if (req.body.requestType == 'getCode') {
    try {
      var status = 0;
      log(`MFA Setup -- User ID: ${req.body.userId}`)
      const userId = req.body.userId;
      log("Getting user");
      const user = await users.get(userId);
      log("Getting member");
      const member = await db.getDocument("maindb", "member", userId);

      if (!user) {
        return res.json({ type: "error", message: "User not found" });
      }
Steven
23 Feb, 2024, 19:09

where is this line logged?

TypeScript
{"code":400,"type":"","response":"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n         \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n <head>\n  <title>400 Bad Request</title>\n </head>\n <body>\n  <h1>400 Bad Request</h1>\n </body>\n</html>\n"}
ZachHandley
23 Feb, 2024, 19:35

it seems to be from the user = await users.get based on the logs

ZachHandley
23 Feb, 2024, 19:37

it's inside logs

Steven
23 Feb, 2024, 19:41

what's your catch block doing?

ZachHandley
23 Feb, 2024, 19:44
TypeScript
} catch (err) {
      log(err);
      return res.json(err);
    }
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