Back

[Solved] console.log in appwrite function

  • 0
  • Functions
  • Web
zwarag\\harrys
21 Apr, 2023, 22:02

Hi I'm trying out appwrite functions. So I created the following function with appwrite function init and deployed it with appwrite function deploy. However I cannot see the log seems to be empty?

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

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

  const account = new sdk.Account(client);
  const database = new sdk.Databases(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);
    console.log(req); // I'd have hoped this logs?
  }

  res.json({
    areDevelopersAwesome: true,
  });
};
TL;DR
The user created an Appwrite function but can't see the console.log output. They tried logging 'req' but it's not working. The user added the log in the wrong place. Instead of logging 'req' inside the if block, they should log it outside the if-else block. Here is the corrected code: ```js const sdk = require("node-appwrite"); module.exports = async function (req, res) { const client = new sdk.Client(); const account = new sdk.Account(client); const database = new sdk.Databases(client); const teams = new sdk.Teams(client); const
Binyamin
21 Apr, 2023, 22:19

From what I know regular logs aren't being return. What I do is adding the stuff I want to log in the res.json

TypeScript

 res.json({
   req:req, 
});
zwarag\\harrys
21 Apr, 2023, 22:20

ok cool, I'll try that

Binyamin
21 Apr, 2023, 22:20

<:appwriteupvote:899677724615016528>

zwarag\\harrys
28 Apr, 2023, 21:00

[Solved] console.log in appwrite function

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