我对 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" } ]重要的一点可能是这里的这一行: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... undefinedError: nullInfo: End process (12:03:39 p.m.)我希望输出是 csv 格式的文本,但正如您从上面看到的那样,我没有定义。我认为我使用它不正确,但并没有真正了解这些东西的经验来理解我的错误。
1 回答

开满天机
TA贡献1786条经验 获得超13个赞
的参数.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或使用回调中的变量。
添加回答
举报
0/150
提交
取消