Back

[SOLVED] Issue: no log output on error

  • 0
  • Functions
TheIlluminatiMember
25 Apr, 2023, 14:58

I have tried this on multiple machines with a fresh install of AppWrite: Based on example on https://appwrite.io/docs/server/databases?sdk=nodejs-default#databasesCreate

TypeScript
const sdk = require("node-appwrite");

module.exports = async function (req, res) {
  const client = new sdk.Client();
  const database = new sdk.Databases(client);

  if (
    !req.variables['APPWRITE_FUNCTION_ENDPOINT'] ||
    !req.variables['APPWRITE_FUNCTION_API_KEY']
  ) {
    // Changed 'console.warn()' to 'console.log()' or else I would never see this message.
    console.log("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 promise = database.create(sdk.ID.unique(), 'test');
  
  // This part does not output anything :( (see attached images)
  promise.then(function (response) {
      console.log(response);
  }, function (error) {
      console.log(error);
  });

  res.json({
    areDevelopersAwesome: true,
  });
};

Am I missing something?

TL;DR
The user was facing an issue with no log output on error. The solution is to avoid using `.then()` and `.catch()` and instead use `async` and `await` in combination with a try/catch block. The user should throw errors to intentionally fail the function and it will be listed under the "Errors" tab. They provided an example code snippet. Additionally, the user mentioned that console log logs don't appear in the logs/errors tabs, but it will be implemented in the future. They provided another code snippet and asked if they were missing something.
Binyamin
25 Apr, 2023, 15:00

Console log logs don't appear in the logs/errors tabs It will be implented in future You can see more here https://github.com/appwrite/appwrite/discussions/5016 at the section Logging

TheIlluminatiMember
25 Apr, 2023, 15:01

Ok! I will keep that in mind, thanks for the fast reply.

Binyamin
25 Apr, 2023, 15:01

For now what I'm doing is to add the logs back to the request when I'm developing, like so

TypeScript
  let logs = '';
  // This part does not output anything :( (see attached images)
  promise.then(function (response) {
    logs = response;
  }, function (error) {
    logs = error;
  });

  res.json({
    logs: logs,
    areDevelopersAwesome: true,
  });
TheIlluminatiMember
25 Apr, 2023, 15:02

Great, I will copy your solution for now. I thought it may have been a bug :p

TheIlluminatiMember
25 Apr, 2023, 15:02

This "Issue" is resolved!

joeyouss
25 Apr, 2023, 15:05

Please mark it as resolved @TheIlluminatiMember

TheIlluminatiMember
25 Apr, 2023, 15:07

I closed it, is that ok?

Binyamin
25 Apr, 2023, 15:08

You can edit the title and [Solved] - before it

TheIlluminatiMember
25 Apr, 2023, 15:08

[SOLVED] Issue: no log output on error

TheIlluminatiMember
25 Apr, 2023, 15:34

This may help anyone facing the same issue. Avoid using .then() and .catch()! Use async, await in combination with a try/catch block:

TypeScript
try {
  const response = await database.create(sdk.ID.unique(), 'test');
  console.log(response)
} catch (error) {
  console.log(error)
}

Logging like this will work. In addition: throwing errors will (intentionally) fail the function and will be listed under the "Errors" tab. See attached image for example.

TypeScript
if (!payload.name) {
  throw new Error('No name was supplied, aborting...');
}
treats
4 May, 2023, 14:20

This should be included in the sample files in the docs.

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