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);
};
I am having problem with executing code synchronously, for loop should execute first and then response should be given.please provide me solution using async/await if possible. this upload api takes multiple files as input. also its converted from csv to json using fs and csvjson packages.
You are doing callback style call inside for loop which doesn't work you expect it to work. You need to wrap it in promise or use promisified version of fs. Something like this
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);
};
Hope this helps. You might need to change few things here or there but you will get an idea.