0

If i have this code

ArbFileProvider.js

const fs = require("fs");

class ArbFile {
  constructor(arbFile) {
    this.arbFile = arbFile;
  }
  async read() {
    const arbFileContents = await fs.promises.readFile(this.arbFile);
    this._arbFileObject = JSON.parse(arbFileContents.toString());
    console.log(this._arbFileObject);
    this._getRessources();
    console.log(JSON.stringify(this.ressources));
    console.log(JSON.stringify(this.properites));
  }

  _getRessources() {
    const ressourcesKeys = Object.keys(this._arbFileObject).filter(
      key => key.match(/^@{1,2}/g) == undefined
    );
    console.log("ressourcesKeys", ressourcesKeys);
    this.ressources = new Map();
    ressourcesKeys.forEach(key => {
      this.ressources.set(key, {
        value: this._arbFileObject[key],
        ...(this._arbFileObject[
          Object.keys(this._arbFileObject).find(val => val == `@${key}`)
        ] || {})
      });
    });
    console.log("ressources", JSON.stringify(this.ressources));

    const propertiesKeys = Object.keys(this._arbFileObject).filter(
      key => key.match(/^@@/g) != undefined
    );
    this.properites = new Map();
    propertiesKeys.forEach(key => {
      this.properites.set(key.replace(/^@@/g, ""), this._arbFileObject[key]);
    });
  }
}

module.exports = ArbFile;

at that point

console.log(JSON.stringify(this.ressources));

this.ressources is an empty map. I don't know why. In the debugger I can clearly see, that is has 55 entries. I have already tried to log for example the Object.keys() of it, but it is an empty Array too.

The arbFile parameter at the constructor is an path to an json-like ARB file.

To call that class, I create an new instance of ArbFile and then call read() on it.

  • Can you include the full `console.log` output (trimmed if it's huge)? – user2740650 Jan 05 '20 at 15:56
  • Where/how are you observing that `this.ressources` is an empty map? Are you correctly `await`ing the `.read()` method? – JLRishe Jan 05 '20 at 16:07
  • Pastebin of the debug console: https://pastebin.com/MAdMyXJu @user2740650 – Lars ReaktoringHD Klette Jan 05 '20 at 16:13
  • @JLRishe as I know, i do not need to await the .read() method if I am not using any data outside of the function. The _getRessources() method is called at the end of the read() method. I am logging this.ressources to get the contents of it. – Lars ReaktoringHD Klette Jan 05 '20 at 16:14
  • @LarsReaktoringHDKlette Even if you are not using anything outside of the function, you should still verify that it completes successfully either using `await` or `.then()`. Otherwise, you could have unhandled rejections. However, I can see from the answer you posted that your issue is with `JSON.stringify()`. One option available here is to use `JSON.stringify([...m])` – JLRishe Jan 05 '20 at 16:59

1 Answers1

1

Well here's why. This is Chrome console output. I'm really surprised, but apparently you cannot JSON.stringify a map!

var m = new Map();
m.set("a", undefined)
Map(1) {"a" => undefined}
m.set("b", "B")
Map(2) {"a" => undefined, "b" => "B"}
m
Map(2) {"a" => undefined, "b" => "B"}
JSON.stringify(m)
"{}"

Instead, try just logging the map directly without JSON.stringify.

user2740650
  • 1,723
  • 12
  • 19