0

For a project, I am running a NodeJS app using Express. I wanted to organize my code and put some objects in a module I am calling with a require.

Something like var xlsImport = require(path.join(__dirname, '../config/xlsImport.js'))(mongoose, mongooseAppUsers, mongooseNodeJS_Test);

My module looks like :

    module.exports = function (mongoose,mongooseAppUsers,mongooseNodeJS_Test) {
    var module={};
(...)
    module.convertField = {
(...)
        "Jeu":                     "game",
        "Démo":                    "demo",
(...)
    return module;
};

In the main file,

xlsImport.convertField['Jeu'] is "game"
xlsImport.convertField['Démo'] is undefined

In the module, both are OK !

I suspect a copy mechanism to be involved...

Any suggestion for solving that problem ? (and some hints about the why ?)

3Pi
  • 31
  • 5
  • When you require('yourModule'), what you get back is the function you wrote there -- not the module object inside that fuction. Are you taking this into account? (nm, I only just noticed that "Jeu" worked). – Semicolon Apr 16 '15 at 18:14
  • I'm not sure what id does imply (and, as you said, it does work for other keys)... – 3Pi Apr 16 '15 at 18:26
  • 1
    Accented characters for JSON key names works in this test: http://jsfiddle.net/ds686eeh/1/ – bloodyKnuckles Apr 16 '15 at 19:08
  • Did you try cutting and pasting the string in both places to make absolutely sure they are identical? It might be possible that the character looks the same but is actually different. – CatDadCode Apr 16 '15 at 19:15
  • I ran a quick test with your code and `xlsImport.convertField['Démo']` came through just fine. – bloodyKnuckles Apr 16 '15 at 19:19
  • I did the same as @bloodyKnuckles. I ran a local test with [these two files](https://gist.github.com/chevex/33a031d96c9c81d163a9) and there was no problem. Have you actually ran the simplified code in your question? I suspect there is something else going on that is not shown in the question, especially since you have so many `(...)` present in your code sample ;) – CatDadCode Apr 16 '15 at 19:21
  • Just to be safe I'd also rename your `var module` to something else. It's probably fine since you override it only in that function scope (it didn't cause an issue in my test), but it would still be good practice to not declare a variable that already exists. – CatDadCode Apr 16 '15 at 19:22
  • PS - there's no reason to call `path.join` if you're going to use forward slashes in the second argument instead of breaking it up into multiple arguments ;) `path.join(__dirname, '..', 'config', 'xlsImport')` – CatDadCode Apr 16 '15 at 19:25
  • Thanks for your interest and help. I was maybe a little bit unclear. Before moving code into a module, everything was OK (no problem with accent in keys). After moving, there no problem inside the module, only in the main where I import with require. This is the reason why I suspect a copy mechanism : only keys with accents (or characters like '€') won't work!!! Is that bound to the javascript engine in nodejs, the code in the require module or something else ? – 3Pi Apr 16 '15 at 19:30

1 Answers1

0

I got it !

It all comes form my editor... When I moved my code from the main file to another newly created, the file encoding was different (windows-1252 instead of UTF-8 -I don't know why-). The accented characters were not the same depending on when they were written.

No strange javascript behavior...

I hope this could help somebody else in the future...

3Pi
  • 31
  • 5
  • I assume you are using WebStorm v10 ... all recent builds have this strange behaviour -- "System Default" and sometimes "UTF-8" got changed to "Windows-1252" ... – LazyOne Apr 17 '15 at 09:49
  • Follow this ticket then (star/vote/comment) to get notified on progress: https://youtrack.jetbrains.com/issue/IDEA-138829 – LazyOne May 15 '15 at 15:28