0
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 循环,然后再给出响应。如果可能,请为我提供使用 async/await 的解决方案。此上传 api 将多个文件作为输入。还使用 fs 和 csvjson 包将其从 csv 转换为 json。

4

1 回答 1

1

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

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


希望这可以帮助。您可能需要在这里或那里更改一些东西,但您会有所了解。

于 2020-02-06T09:48:57.927 回答