错误:通过 Postman 上载json文件时读取EconReset请求正文txt:undefined

eh57zj3b  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(192)

我试图上传一个json文件来读取数据,然后将其保存在我的mongodb数据库中,我使用multer这样做,但是postman返回以下错误 Error: read ECONNRESET 上面写着 Request Body txt: undefined 有时数据保存在数据库中,有时数据不存在如何修复此错误?
以下是我正在使用的代码:

app.post("/upload-matching-config", uploads.single("txt"), async (req, res) => {
  // Read the file and send to the callback
  fs.readFile(req.file.path, handleFile);
  let obj;
  let DataSourceID_1;
  let DataSourceID_2;
  let jsonData_1 = [];
  let jsonData_2 = [];
  let DSID_1;
  let DSID_2;
  // Write the callback function
  async function handleFile(err, data) {
    try {
      obj = JSON.parse(data);
      DataSourceID_1 = obj.DataSourceID_1;
      DataSourceID_2 = obj.DataSourceID_2;
      console.log(DataSourceID_1);
      console.log(DataSourceID_2);
    } catch (err) {
      console.log("Something went wrong!" + err);
    }
    try {
      jsonData_1 = await Source.findOne({
        DataSource_ID: DataSourceID_1,
      });
      DSID_1 = jsonData_1._id;
    } catch (err) {
      console.log("Something went wrong!" + err);
    }

    try {
      jsonData_2 = await Source.findOne({
        DataSource_ID: DataSourceID_2,
      });
      DSID_2 = jsonData_2._id;
    } catch (err) {
      console.log("Something went wrong!" + err);
    }
    console.log(DSID_1);
    console.log(DSID_2);

    const matching = new Matching({
      DSID_1: DSID_1,
      DSID_2: DSID_2,
      DataSourceID_1: DataSourceID_1,
      DataSourceID_2: DataSourceID_2,
      matchingRules: obj.matchingRules,
      MatchingReport_Content: obj.MatchingReport_Content,
      DataSource_1_UnmatchedReport_Content:
        obj.DataSource_1_UnmatchedReport_Content,
      DataSource_2_UnmatchedReport_Content:
        obj.DataSource_2_UnmatchedReport_Content,
    });
    try {
      await matching.save().then((data) => {
        res.send(data);
      });
    } catch (err) {
      console.log(err);
      res.status(400).json({
        message: "DataSources you are trying to match does not exist!",
      });
    }
  }
});

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题