michaelkourlas / node-js2xmlparser

Popular Node.js module for parsing JavaScript objects into XML
https://www.npmjs.org/package/js2xmlparser
Apache License 2.0
221 stars 52 forks source link

TypeError: str.charCodeAt is not a function #79

Closed mraak closed 4 years ago

mraak commented 5 years ago

What could possibly be wrong here? I'm using it in Node environment.

function createManifest(){
  const imsData = {
    "manifest":{
      "a":"b"
    }
  console.log(imsData)  
  const imsDataXml = js2xmlparser.parse(imsData) // line 88
  console.log(imsDataXml)
}

And the output

{ manifest: { a: 'b' } }
C:\dev\projects\f1\f1\node_modules\xmlcreate\lib\validate.js:116
    var initialFirstChar = str.charCodeAt(0);
                               ^

TypeError: str.charCodeAt is not a function
    at Object.validateName (C:\dev\projects\f1\f1\node_modules\xmlcreate\lib\validate.js:116:32)
    at XmlElement.set [as name] (C:\dev\projects\f1\f1\node_modules\xmlcreate\lib\nodes\XmlElement.js:93:54)
    at new XmlElement (C:\dev\projects\f1\f1\node_modules\xmlcreate\lib\nodes\XmlElement.js:73:19)
    at XmlDocument.element (C:\dev\projects\f1\f1\node_modules\xmlcreate\lib\nodes\XmlDocument.js:116:23)
    at Object.parse (C:\dev\projects\f1\f1\node_modules\js2xmlparser\lib\main.js:286:32)
    at createManifest (C:\dev\projects\f1\f1\src\node\scorm.js:88:35)
    at Object.<anonymous> (C:\dev\projects\f1\f1\src\node\scorm.js:125:1)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
mikailcolak commented 5 years ago

It seems this is not a bug, it is just an misunderstanding of the API I guess, you should provide the root element name at the first argument of the parse function and after that you should provide the actual object.

Can you please try this:

const imsDataXml = js2xmlparser.parse('imsData', imsData);
// or
const rootElement = Object.keys(imsData)[0];
const imsDataXml = js2xmlparser.parse(rootElement, imsData[rootElement]);