Back

[SOLVED] Node Function Map log is always empty

  • 0
  • Functions
Super_Nove
16 Sep, 2023, 18:20

I have an appwrite function where i define a map and add some keys and values. For testing purposes i tried to log the map but the problem is that the result of the log is always '{}' even if the map is filled.

i just created a node 18 function where i pasted this code.

TypeScript
  const m = new Map();
  // result is '{}' as expected
  console.log('m:');
  console.log(m);
  m.set("a", 1);
  m.set("b", 2);
  m.set("c", 3);
  // result is '{}' which is not expected.
  // I am expecting '{a: 1, b: 2, c: 3}'
  console.log('m:');
  console.log(m);

  // result is '[1,2,3]' as expected
  console.log("Array.from(m.values())");
  console.log(Array.from(m.values()));

  // result is '[a,b,c]' as expected
  console.log("Array.from(m.keys())");
  console.log(Array.from(m.keys()));

the result of the appwrite console is:

TypeScript
m:
{}
m:
{} <-- this should be filled while logging
Array.from(m.values())
[1,2,3]
Array.from(m.keys())
["a","b","c"]
m.get("a")
1
m.has("a"):
true

I find this behaviour very strange and unexpected. I am logging the map in an larger function for developing purpose. Is this behaviour expected?

Note: I am still on appwrite version 1.3.7

TL;DR
Title: [SOLVED] Node Function Map log is always empty The issue is with using `JSON.stringify()` on objects within the map. Some objects don't handle that well. To print out the map, you can use `console.log(Object.fromEntries(m))`. This should display the keys and values correctly. The empty logging behavior is expected. Note: The issue was resolved by using `console.log(Object.fromEntries(m))`.
Drake
16 Sep, 2023, 18:52

The logs use JSON.stringify() on objects but some objects don't handle that well. That's probably what's going on. You can try doing m.toString() or you'd have to manually print out the map.

Super_Nove
16 Sep, 2023, 19:07

Thank you very much.

JSON.stringify() just outputs [object Object]. What works is this: console.log(Object.fromEntries(m))

Drake
16 Sep, 2023, 19:34

[SOLVED] Node Function Map log is always empty

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