【问题标题】:How to properly use the json-2-csv node module?如何正确使用 json-2-csv 节点模块?
【发布时间】:2019-10-28 07:01:48
【问题描述】:

我对 nodeJS 很陌生,所以也许我做错了什么,但我正在尝试使用节点模块 json-2-csv。但是当我按照文档并尝试使用它时,我的结果是未定义的。

这是我尝试使用测试数据的代码。另外,我已经运行 npm install json-2-csv 并检查了我的 package.json 以查看它是否存在(版本 3.5.4)

let converter = require('json-2-csv');


var array = [
    {
        "configurationId": {
        "id": "4000",
        "displayName": "4000",
        "uri": "/configuration/users/4000"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    },
    {
        "configurationId": {
        "id": "4001",
        "displayName": "4001",
        "uri": "/configuration/users/4001"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    },
    {
        "configurationId": {
        "id": "4002",
        "displayName": "40002",
        "uri": "/configuration/users/4002"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    }
    ];

var result = converter.json2csv(array, function(err, csv){
    console.log("Error: ", err);
}, expandArrayObjects = true);
console.log("result is...", result);

重要的一点可能是这里的这一行:

var result = converter.json2csv(array, function(err, csv){
    console.log("Error: ", err);
}, expandArrayObjects = true);

这是我的输出:

Info: Start process (12:03:39 p.m.)
the result is... undefined
Error:  null
Info: End process (12:03:39 p.m.)

我希望输出是 csv 格式的文本,但正如您从上面看到的那样,我没有定义。我想我用错了,但真的没有这方面的经验来理解我的错误。

【问题讨论】:

    标签: javascript node.js json csv


    【解决方案1】:

    .json2csv 的参数是 (csv, function, options)。根据 [npm 来源]

    options - (Optional) A JSON document specifying any of the following key value pairs:
    

    所以,代码将是:

    var result = converter.json2csv(array, function(err, csv){
        console.log("Error: ", err);
        console.log("Result: ", csv);
    }, {expandArrayObjects:true});
    

    如果您想使用其他设置,options 对象将为:

    {
        expandArrayObjects: true,
        ....
        ....
    }
    

    但我不确定这会奏效,不是因为语法,而是因为我怀疑 json2csv 是否可以将深度 json 转换为 CSV。

    另外,result 不会有数据,因为数据是通过回调传递的,它是一个异步函数。你需要console.log它或者使用回调里面的变量。

    【讨论】:

    • 啊,这种格式更有意义。但是尝试它仍然会给我一个未定义的结果。你可能是对的,但我不妨尝试并希望大声笑,
    • 哦还有一件事,它应该在回调内部,结果将是undefined
    • kk 修复了,你说得对,它奇怪地深入了标题,但没有得到更深层次的对象的值,只是将它们清空。但无论如何,我会接受你的回答,因为它完全回答了我的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多