question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unwind is not working as expected.

See original GitHub issue

json2csv version: ^4.5.2 node version: 10.16.0

const { Parser } = require('json2csv');
const fields = ['judgeName', 'source_product_id', 'target_products.product_id', 'target_products.score', 'time'];
const json2csvParser = new Parser({ fields, unwind: ['target_products'] });
const csvJudges = json2csvParser.parse(judges);

My data: judes:

[
    {
        "_id": "5d5a5170f9acfe362c61272c",
        "judgeName": "hunnyjmjim",
        "source_product_id": "323",
        "target_products": [
            {
                "_id": "5d5a5170f9acfe362c61272e",
                "product_id": "9096",
                "score": 1
            },
            {
                "_id": "5d5a5170f9acfe362c61272d",
                "product_id": "48484",
                "score": 2
            }
        ],
        "time": "2019-08-19T07:36:16.433Z",
        "__v": 0
    },
    {
        "_id": "5d5a83c91c9d440000e258df",
        "judgeName": "hunnyjmjimq",
        "source_product_id": "323",
        "target_products": [
            {
                "_id": "5d5a5170f9acfe362c61272e",
                "product_id": "9096",
                "score": 1
            },
            {
                "_id": "5d5a5170f9acfe362c61272d",
                "product_id": "48484",
                "score": 2
            }
        ],
        "time": "2019-08-12T07:36:16.433Z",
        "__v": 0
    },
    {
        "_id": "5d5b8a14c5c38f450ca39fb5",
        "judgeName": "saitrinath",
        "source_product_id": "448",
        "target_products": [
            {
                "_id": "5d5b8a14c5c38f450ca39fba",
                "product_id": "60015",
                "score": 3
            },
            {
                "_id": "5d5b8a14c5c38f450ca39fb9",
                "product_id": "973",
                "score": 2
            },
            {
                "_id": "5d5b8a14c5c38f450ca39fb8",
                "product_id": "81851",
                "score": 1
            },
            {
                "_id": "5d5b8a14c5c38f450ca39fb7",
                "product_id": "70898",
                "score": 3
            },
            {
                "_id": "5d5b8a14c5c38f450ca39fb6",
                "product_id": "274",
                "score": 1
            }
        ],
        "time": "2019-08-20T05:50:12.613Z",
        "__v": 0
    }
]

I am getting output like this:

"judgeName","source_product_id","target_products.product_id","target_products.score","time"
,,"9096",1,
,,"48484",2,
,,"9096",1,
,,"48484",2,
,,"60015",3,
,,"973",2,
,,"81851",1,
,,"70898",3,
,,"274",1,

When I use unwind command, other fields(“judgeName”,“source_product_id”, “time”) are getting missing which is not expected.

Just check the issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
andy-welocommented, Jan 24, 2020

For anyone else having this kind of problem, it has something to do with mongoose documents. I found a way to fix this by using toObject() at every item. So for your example i guess it’d be fixed by changing:

const csvJudges = json2csvParser.parse(judges); to const csvJudges = json2csvParser.parse(judges.map(judge => judge.toObject()));

Hope this helps

0reactions
OnkarMadlicommented, Jun 9, 2021

For anyone else having this kind of problem, it has something to do with mongoose documents. I found a way to fix this by using toObject() at every item. So for your example i guess it’d be fixed by changing:

const csvJudges = json2csvParser.parse(judges); to const csvJudges = json2csvParser.parse(judges.map(judge => judge.toObject()));

Hope this helps

I had the same problem since I was directly using the data returned by a mongoose query. This solution worked!!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unwind not working as expected · Issue #479 · zemirco/json2csv
juanjoDiaz It looks like Unwind is not working as expected in v 5.0.1 when using nested JSON data I have even looked at...
Read more >
MongoDB unwind aggregation query giving unexpected results
The problem was that the network element format was inconsistent. ... the format across the collection, everything worked as expected.
Read more >
I don't get unwind in aggregation - why do I need it? - Studio 3T
The default answer is that no document is generated at all, because there's nothing to work with. And that's why there's ...
Read more >
Cannot unwind from first chance exceptions
The behavior you are seeing is expected, we do not unwind from first chance exceptions if Just My Code is disabled. There are...
Read more >
Help re-grouping after unwind - Working with Data - MongoDB
I am implementing the Event Sourcing & CQRS patterns. I have a collection of documents with the events from users to manipulate models....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found