Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attributeNameFn callback receives an undefined attributeValue #182

Open
ediblecode opened this issue Feb 4, 2022 · 0 comments
Open

attributeNameFn callback receives an undefined attributeValue #182

ediblecode opened this issue Feb 4, 2022 · 0 comments

Comments

@ediblecode
Copy link

Repro steps:

Provide an attributeNameFn, where the second argument is an attributeValue as per the docs. For example:

xml2js(`<root><element attr="val">content</element></root>`, {
    attributeNameFn: (attributeName, attributeValue, parentElement) => {
        console.log({ attributeValue });

        return attributeName;
    },
});

Expected result

The received attributeValue argument is the value of the current attribute.

In the above example the expected result is logging { attributeValue: 'val' }.

Actual result

The received attributeValue argument in the attributeNameFn is undefined.

Further details

Looks like it's being caused by:

xml-js/lib/xml2js.js

Lines 150 to 154 in f0376f2

if ('attributeNameFn' in options) {
var temp = attributes[key];
delete attributes[key];
attributes[options.attributeNameFn(key, attributes[key], currentElement)] = temp;
}

Notice the value is being deleted before being passed into the function as the second argument. So passing temp here looks like it'd solve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant