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

object mapper returns array with empty object when using transform function #86

Open
martijnsmitt opened this issue Apr 16, 2021 · 4 comments

Comments

@martijnsmitt
Copy link

Hi! I'm using object-mapper for simplifying objects and came across a bug(?) or at least an unexpected result. When using the transform function the mapper wil always create a empty object. Check the example below

  const data = {
    id: 123,
    body: 'Hello World',
  }
  const config = {
    'title': {
      key: 'shouldNotExist[].title',
      transform: id => id,
    },
    'body': 'message',
  }
  const result = objectMapper(data, config)
  // Results in
  // {
  //   shouldNotExist: [ {} ],
  //   message: 'Hello World'
  // }

I expected that the result wouldn't return shouldNotExist, because there is no title, but instead object-mapper returns an array with empty object. The example above is a minimal version of the real code, but isn't it possible to just return nothing and don't create an array if there is no data?

@zeabdelkhalek
Copy link

I have the same issue

@wp55
Copy link

wp55 commented Jul 8, 2021

Has someone found a solution for this problem?

@martijnsmitt
Copy link
Author

Has someone found a solution for this problem?

unfortunately not. Have you found another npm package that solved your issue? Because I'm still looking for a good object to object mapping utility

@GauravChinavle
Copy link

GauravChinavle commented Jul 7, 2022

{ 
           "sourceObject": {
                "key": "destObject",
                transform: function (value,source,dest) {
                    if(Array.isArray(source.rootKey) && source.rootKey.length == 0) {
                        return [];
                    } else {
                        return [...dest.resultingArray];
                    }
                },
                default: []
            }
 }

It worked for me. but make sure you put it at the end of the key mapping
In response i was getting and
{ response: { data: { resultingArray: [] } } }
and what object mapper was doing
{ response: { data: { resultingArray: [ {} ] } } }

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

4 participants