Back

[SOLVED] Nothing on the console.log

  • 0
  • Web
gelmuda
10 Jun, 2023, 12:50

Hi, I wrote a simple app for Functions, just to get and list the attributes, but I cannot get anything on the console, app is finishing OK (green light). apikey has enabled all permissions.

const sdk = require('node-appwrite');

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

TypeScript
client
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('6411d...8abc')
    .setKey('1cc9a39ba9f39f93...de2eac30e3')
    //.setSelfSigned(true);
;

const database = new sdk.Databases(client);

const promise = database.listAttributes('648...342', '648...859').then(
    (response) => {
        console.log(response);
        response.attributes.forEach((val) => {
            console.log(val.key);
            });
    },
    (err) => {
        console.log(err);
        res.json({ error: err });
    }
);

promise.then(function (response) {
    console.log(response);
}, function (error) {
    console.log(error);
});

res.json({ completed: true }); };

TL;DR
The user was unable to see anything on the console.log despite their code running successfully. Another user suggested using async/try/await/catch instead of then, or await the promise or call res.json inside the callbacks. The user also mentioned that when they ran the code locally in VSC, the console.log worked. The original poster confirmed that the solution worked for them.
gelmuda
10 Jun, 2023, 13:06

Also, when I run body only of the function() locally in VSC, I get what I want on console.log.

Drake
10 Jun, 2023, 15:37

Your function is probably completing before the console.log() gets executed. I highly suggest using async/try/await/catch instead of then. Either that, or you can await your promise or call res.json inside the callbacks.

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

gelmuda
10 Jun, 2023, 15:38

Thank you. I will check this now.

gelmuda
10 Jun, 2023, 16:15

This did the trick, thanks again.

TypeScript
try {
  const promise = await database.listAttributes('6483...342', '648...859');
  promise.attributes.forEach((val) => {
    console.log(val.key);
  });
} catch(error) {
  res.json({ error: err });
}
gelmuda
10 Jun, 2023, 16:16

[SOLVED] Nothing on the console.log

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