Node.js 在nodejs中将多个csv文件同步转换为json
我在同步执行代码时遇到问题,因为循环应该先执行,然后给出响应。如果可能,请使用async/await为我提供解决方案。此上载api接受多个文件作为输入。此外,它还可以使用fs和csvjson包从csv转换为json。您在for循环内部执行回调风格的调用,但这不起作用,您希望它能起作用。您需要将其包装在promise中,或者使用promisified版本的fs。像这样的Node.js 在nodejs中将多个csv文件同步转换为json,node.js,async-await,es6-promise,csvtojson,Node.js,Async Await,Es6 Promise,Csvtojson,我在同步执行代码时遇到问题,因为循环应该先执行,然后给出响应。如果可能,请使用async/await为我提供解决方案。此上载api接受多个文件作为输入。此外,它还可以使用fs和csvjson包从csv转换为json。您在for循环内部执行回调风格的调用,但这不起作用,您希望它能起作用。您需要将其包装在promise中,或者使用promisified版本的fs。像这样的 exports.upload = async (req, res) => { if (!req.files) {
exports.upload = async (req, res) => {
if (!req.files) {
ResponseHandler.notFound(res, ERROR.ALL_FIELD_REQUIRED);
}
const files = req.files;
for(let file of files){
const csvFilePath = file.path;
fs.readFile(csvFilePath, 'utf-8', async (err, fileContent) => {
if (err) {
return ResponseHandler.internalServerError(
res,
ERROR.INTERNAL_SERVER_ERROR
);
}
const options = {
delimiter: ',',
quote: '"'
};
const jsonObj = csvjson.toObject(fileContent, options).map(element => {
return {
guid: element.guid || null,
name: element.name || null,
whenChanged: element.whenChanged || null,
whenCreated: element.whenCreated || null,
co: element.co || null,
company: element.company || null,
givenName: element.givenName || null,
sn: element.sn || null,
profileImage: element.profileImage || null,
designation: element.designation || null,
jobTitle: element.jobTitle || null,
department: element.department || null,
ward: element.ward || null,
site: element.site || null,
region: element.region || null,
offer: element.offer || null,
isAppUser: element.isAppUser || null
};
});
try {
await UserService.createUsers(jsonObj);
} catch (err) {
return ResponseHandler.internalServerError(
res,
ERROR.INTERNAL_SERVER_ERROR
);
}
});
}
return ResponseHandler.send(res, STATUS.SUCCESS, SUCCESS.FILE_UPLOADED);
};
希望这有帮助。你可能需要在这里或那里改变一些事情,但你会有一个想法。你面临的问题是什么?
const fs = require("fs").promises;
exports.upload = async (req, res) => {
if (!req.files) {
ResponseHandler.notFound(res, ERROR.ALL_FIELD_REQUIRED);
}
const files = req.files;
for (const file of files) {
const csvFilePath = file.path;
// eslint-disable-next-line no-loop-func
const fileContent = await fs.readFile(csvFilePath, "utf-8");
const options = {
"delimiter": ",",
"quote": "\""
};
// eslint-disable-next-line complexity
const jsonObj = csvjson.toObject(fileContent, options).map(element => {
return {
"guid": element.guid || null,
"name": element.name || null,
"whenChanged": element.whenChanged || null,
"whenCreated": element.whenCreated || null,
"co": element.co || null,
"company": element.company || null,
"givenName": element.givenName || null,
"sn": element.sn || null,
"profileImage": element.profileImage || null,
"designation": element.designation || null,
"jobTitle": element.jobTitle || null,
"department": element.department || null,
"ward": element.ward || null,
"site": element.site || null,
"region": element.region || null,
"offer": element.offer || null,
"isAppUser": element.isAppUser || null
};
});
try {
await UserService.createUsers(jsonObj);
// eslint-disable-next-line no-catch-shadow
} catch (err) {
return ResponseHandler.internalServerError(
res,
ERROR.INTERNAL_SERVER_ERROR
);
}
}
return ResponseHandler.send(res, STATUS.SUCCESS, SUCCESS.FILE_UPLOADED);
};