[英]JSON.Parse in JS is returning undefined - (Node.js app)
我創建了一個 function 以在 CSV 中轉換 JSON。但是 JSON.parse 返回未定義。 請有人幫忙。
function JSONtoCSVConverter(JSONDataPath, showLabels) {
var JSONData = fs.readFile(JSONDataPath, "utf-8", (err, fileContent) => {
if (err) {
console.log(err);
} else {
console.log(fileContent);
JSON.parse(fileContent.toString().trim());
}
});
console.log(JSONData);
if (showLabels) {
console.log(JSONData.slice(1, JSONData.length));
var rowsOfDataArray = Object.keys(JSONData[0]);
var rowsOfData = rowsOfDataArray.join(",") + "\n";
}
JSONData.forEach((object) => {
var cellsOfDataArray = Object.values(object);
cellsOfData = cellsOfDataArray.join(",") + "\n";
});
var csv = rowsOfData + "\n" + cellsOfData;
return csv;
}
但是,我收到的錯誤是:
undefined TypeError: Cannot read properties of undefined (reading 'slice') at JSONtoCSVConverter (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\routes\reports.js:45:26) at C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\routes\reports.js:26:7 at Layer.handle [as handle_request] (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\node_modules\express\lib\router\route.js:144:13) at Route.dispatch (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\node_modules\express\lib\router\route.js:114:3) at Layer.handle [as handle_request] (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\node_modules\express\lib\router\layer.js:95:5) at C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\node_modules\express\lib\router\index.js:284:15 at Function.process_params (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\node_modules\express\lib\router\index.js:346:12) at next (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\node_modules\express\lib\router\index.js:280:10) at urlencodedParser (C:\Users\harsh\OneDrive\Documents\Visual Studio 2022\Web Development\contactStoreApp\node_modules\body-parser\lib\types\urlencoded.js:91:7)
JSON.parse
返回未定義或fs.readFile
返回undefined
? fs.readFile
不應該返回任何東西
如果你想正確使用fs.readFile
的結果,你必須將代碼的 rest 放在回調中,如下所示:
function JSONtoCSVConverter(JSONDataPath, showLabels) {
fs.readFile(JSONDataPath, "utf-8", (err, fileContent) => {
if (err) {
console.log(err);
} else {
console.log(fileContent);
var JSONData = JSON.parse(fileContent.toString().trim());
console.log(JSONData);
if (showLabels) {
console.log(JSONData.slice(1, JSONData.length));
var rowsOfDataArray = Object.keys(JSONData[0]);
var rowsOfData = rowsOfDataArray.join(",") + "\n";
}
JSONData.forEach((object) => {
var cellsOfDataArray = Object.values(object);
cellsOfData = cellsOfDataArray.join(",") + "\n";
});
var csv = rowsOfData + "\n" + cellsOfData;
console.log(csv)
}
});
}
您可能已經注意到,這非常混亂 - 這種模式被稱為回調地獄,並且是 JavaScript 中使用非阻塞 API 時的一種較舊的編程風格。 您還會注意到我沒有return
變量csv
- 為此,您必須將回調JSONtoCSVConverter
作為第三個參數傳遞給 JSONtoCSVConverter。
這是一個這樣做的例子(它被稱為回調模式)。
function JSONtoCSVConverter(JSONDataPath, cb) {
fs.readFile(JSONDataPath, "utf-8", (err, fileContent) => {
if (err) {
cb(err, undefined);
} else {
var JSONData = JSON.parse(fileContent.toString().trim());
cb(null, JSONData);
}
});
}
// use the function like this:
JSONtoCSVConverter('./file.json', function(err, data) {
if (err) {
console.error(err);
} else {
console.log('here is JSON data:')
console.log(data);
}
});
但是,有一個更好的解決方案。
更好的解決方案涉及使用承諾。 最新版本的 NodeJS 具有基於 Promise 的 API(例如,請參閱fs.promises.readFile ,它允許您像這樣使用它們:
const fs = require('fs')
async function JSONtoCSVConverter(JSONDataPath, showLabels) {
var fileContent = await fs.promises.readFile(JSONDataPath, "utf-8")
console.log(fileContent);
var JSONData = JSON.parse(fileContent.toString().trim());
console.log(JSONData);
if (showLabels) {
console.log(JSONData.slice(1, JSONData.length));
var rowsOfDataArray = Object.keys(JSONData[0]);
var rowsOfData = rowsOfDataArray.join(",") + "\n";
}
JSONData.forEach((object) => {
var cellsOfDataArray = Object.values(object);
cellsOfData = cellsOfDataArray.join(",") + "\n";
});
var csv = rowsOfData + "\n" + cellsOfData;
return csv;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.