NodeJS React js wait函数返回错误

mi7gmzs6  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(127)

我有一个handlesSubmitasync函数来将数据发送到后端Node JS服务器,该服务器接收数据并将其提交到Postgres数据库。

const handleSubmitData = async (event) => {
    event.preventDefault();
    event.target.disabled = true;
    try {
      const body = {
        well_id: well_id,
        date_install_pump: dateform2,
        pump_type: pump_type,
        pump_size: pump_size,
        stroke_length: stroke_length,
        barrel_length: barrel_length,
        gas_anchor: gas_anchor,
        pump_set: pump_set,
        manufacturer: manufacturer,
        installation_remarks: installation_remarks,
        date_install_pu: dateSRP1,
        pumping_unit_type: pumping_unit_type,
        pumping_unit_model: pumping_unit_model,
        motor_hp: motor_hp,
        manufacturer_surf: manufacturer,
        installation_remarks_surf: installation_remarks,
        row_changed_by: users,
        row_created_by: users,
        username: users,
        role: role,
      }
      const response = await axios.post(global.config.i18n.url + 'dw_pump', body);
      if (response && response.data && response.data.message) {
        setIsSuccess(true);
        setModals(response.data.message);
        setOpenModal(true);
      } else {
        setIsSuccess(false);
        setModals('Please check the show all tables page to see your data');
        setOpenModal(true);
      }
    } catch (e) {
      setIsSuccess(false)
      setModals(e.response.data.message)
      setOpenModal(true)
    } finally {
      event.target.disabled = false;
    }
  }

字符串
这是我的后端服务器

async postPumpSRPDownAndSurfTrx(req, res) {
    let trx;
    try {
        trx = await db_dwvm.transaction();
        const momentDate = new Date();
        const pumpHeader = {
            well_id: req.body.well_id,
            date_install_pump: moments(req.body.date_install_pump),
            pump_type: req.body.pump_type,
            contractor: null,
            installation_remarks: null,
            row_changed_by: req.body.row_changed_by,
            row_created_by: req.body.row_created_by,
            created_at: moments(momentDate),
            updated_at: moments(momentDate),
        }
        const pumpSRPDownhole = {
            well_id: req.body.well_id,
            date_install_pump: moments(req.body.date_install_pump),
            pump_type: req.body.pump_type,
            pump_size: req.body.pump_size,
            stroke_length: req.body.stroke_length,
            barrel_length: req.body.barrel_length,
            gas_anchor: req.body.gas_anchor,
            pump_set: req.body.pump_set,
            manufacturer: req.body.manufacturer,
            installation_remarks: req.body.installation_remarks
        }
        const pumpSRPSurface = {
            well_id: req.body.well_id,
            date_install_pump: moments(req.body.date_install_pump),
            date_install_pu: moments(req.body.date_install_pu),
            pumping_unit_type: req.body.pumping_unit_type,
            pumping_unit_model: req.body.pumping_unit_model,
            motor_hp: req.body.motor_hp,
            manufacturer: req.body.manufacturer_surf,
            installation_remarks: req.body.installation_remarks_surf,
            row_changed_by: req.body.row_changed_by,
            row_created_by: req.body.row_created_by,
            created_at: moments(momentDate),
            updated_at: moments(momentDate)
        };
        console.log(pumpHeader)
        console.log(pumpSRPDownhole)
        console.log(pumpSRPSurface);
        const date = new Date();
        const newdate = `${date.toLocaleString('id-ID', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' })}` + " Pukul " + ("0" + ` ${date.toLocaleString('id-ID', { hour: 'numeric' })}`).slice(-2) + ':' + ("0" + `${date.toLocaleString('id-ID', { minute: 'numeric' })}`).slice(-2);
        const { username, role, pesan = `Pump Header, Pump SRP Downhole dan Pump SRP Surface Telah Terinput Pada ${newdate}` } = req.body;
        await models.pump_header.create(pumpHeader, { transaction: trx });
        await models.pump_srp_downhole.create(pumpSRPDownhole, { transaction: trx });
        await models.pump_srp_surface.create(pumpSRPSurface, { transaction: trx });
        await notificationvmModel.postNotification(username, role, pesan);
        await trx.commit();
        res.status(201).json({
            message: 'Success',
            error: false
        });
    } catch (error) {
        if (trx) {
            await trx.rollback();
            res.status(500).send({
                message: error.message,
                error: true,
            });
        }
    }
}


我的问题是,当我按下submit按钮并将数据发送到backend服务器时,服务器确实收到了数据。但即使数据成功提交到数据库,response以某种方式进入了catch部分,因此无法正确显示。我认为发生的情况是数据库没有将response发送到backend,该frontend已经“获取”的response和它发现什么都没有,所以它认为这是一个错误,并移动到catch节.我认为这是因为在后端终端,在它收到数据从前端它显示此错误代码connection closed before res end/flush
这是来自浏览器控制台enter image description here的错误

r6hnlfcb

r6hnlfcb1#

你可以试试下面的:

const handleSubmitData = async (event) => {
    event.preventDefault();
    event.target.disabled = true;
    try {
      const body = {
        well_id: well_id,
        date_install_pump: dateform2,
        pump_type: pump_type,
        pump_size: pump_size,
        stroke_length: stroke_length,
        barrel_length: barrel_length,
        gas_anchor: gas_anchor,
        pump_set: pump_set,
        manufacturer: manufacturer,
        installation_remarks: installation_remarks,
        date_install_pu: dateSRP1,
        pumping_unit_type: pumping_unit_type,
        pumping_unit_model: pumping_unit_model,
        motor_hp: motor_hp,
        manufacturer_surf: manufacturer,
        installation_remarks_surf: installation_remarks,
        row_changed_by: users,
        row_created_by: users,
        username: users,
        role: role,
      }
      const response = await axios.post(global.config.i18n.url + 'dw_pump', body);
      if (response.status === 200) {
        setIsSuccess(true);
        setModals(response.data.message);
        setOpenModal(true);
      } else {
        setIsSuccess(false);
        setModals('Please check the show all tables page to see your data');
        setOpenModal(true);
      }
    } catch (e) {
      setIsSuccess(false)
      setModals(e.response)
      setOpenModal(true)
    } finally {
      event.target.disabled = false;
    }
  }

字符串

相关问题