在nodejs中同步将多个csv文件转换为json

converting multiple csv files to json synchronously in nodejs

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);
};

我在同步执行代码时遇到问题,for 循环应该先执行,然后响应应该是 given.please 如果可能,请使用 async/await 为我提供解决方案。此上传 api 将多个文件作为输入。还使用 fs 和 csvjson 包将其从 csv 转换为 json。

您正在 for 循环中进行回调式调用,但您期望它不会起作用。您需要将其包装在 promise 中或使用 fs 的 promified 版本。像这样

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);
};


希望这对您有所帮助。您可能需要在这里或那里更改一些东西,但您会明白的。