首页 > 解决方案 > 我正在尝试使用 react-redux 存储我的数据,但它没有。我该如何解决这个问题?

问题描述

我正在做一个反应项目,当我尝试使用邮递员存储我的数据时,它工作得很好。但另一方面,如果我使用 react-redux,它不会传递到数据库中。我该如何解决这个问题?以下是一些代码片段。

const mongoose = require('mongoose');

const verifySchema = new mongoose.Schema(
  {
    investment: {
      type: mongoose.Schema.ObjectId,
      ref: 'Investment',
    },
    topup: {
      type: mongoose.Schema.ObjectId,
      ref: 'TopUp',
    },
    verified: {
      type: Boolean,
      required: true,
    },
    verifiedBy: {
      type: mongoose.Schema.ObjectId,
      ref: 'Auth',
    },
    dateVerified: {
      type: Date,
      default: Date.now(),
      required: true,
    },
    note: String,
    createdAt: { type: Date, default: Date.now() },
    paymentDates: [Date],
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

const Verify = mongoose.model('Verify', verifySchema);

module.exports = Verify;

这是提交处理程序

const submitHandler = (e) => {
    e.preventDefault();
    if (isConfirmed === true) {
      const paymentDates = [];
      for (let i = 1; i <= investment.investmentDuration; i += 1) {
        paymentDates.push(
          moment(dateConfirm).add(i, 'months').add(1, 'days').toISOString()
        );
      }
      const data = new FormData();
      data.append('verified', isConfirmed);
      data.append('investment', investment._id);
      data.append('dateVerify', dateConfirm);
      data.append('verifiedBy', userInfo.data.user._id);
      data.append('amount', investment.investmentAmount);
      data.append('duration', investment.investmentDuration);
      data.append('paymentDates', paymentDates);

      data.append('note', note);
      dispatch(verifyInvestment(data));
    }
  };

这是动作

export const verifyInvestment = (formData) => async (dispatch, getState) => {
  try {
    dispatch({ type: VERIFY_CREATE_REQUEST });

    const {
      userLogin: { userInfo },
    } = getState();



    for (var pair of formData.entries()) {
      console.log(pair[0] + ', ' + pair[1]);
    }


    const config = {
      headers: {
        // Authorization: `Bearer ${userInfo.token}`,
        'Content-Type': 'application/json',
      },
    };

    const { data } = await axios.post(`/api/v1/verify`, formData, config);

    dispatch({
      type: VERIFY_CREATE_SUCCESS,
      payload: data,
    });
  } catch (error) {
    dispatch({
      type: VERIFY_CREATE_FAIL,
      payload:
        error.response && error.response.data.message
          ? error.response.data.message
          : error.message,
    });
  }
};

从我的控制台,我打印了这条消息

verified, true
verifyActions.js:16 investment, 5fcde10d1ec7da05d54942ed
verifyActions.js:16 dateVerify, 2020-12-31
verifyActions.js:16 verifiedBy, 5fcc8739b926611a541a5baf
verifyActions.js:16 amount, 400000
verifyActions.js:16 duration, 12
verifyActions.js:16 paymentDates, 2021-01-31T23:00:00.000Z,2021-02-28T23:00:00.000Z,2021-03-31T23:00:00.000Z,2021-04-30T23:00:00.000Z,2021-05-31T23:00:00.000Z,2021-06-30T23:00:00.000Z,2021-07-31T23:00:00.000Z,2021-08-31T23:00:00.000Z,2021-09-30T23:00:00.000Z,2021-10-31T23:00:00.000Z,2021-11-30T23:00:00.000Z,2021-12-31T23:00:00.000Z
verifyActions.js:16 note, Hello World
POST http://localhost:3000/api/v1/verify 500 (Internal Server Error)

我从该州收到此消息。verify(pin):“验证验证失败:已验证:verified需要路径。”

请我真的需要帮助来解决这个问题。如果我尝试使用邮递员发布它,它会存储在数据库中,但对于这种情况,它不会。我不知道在发布数据之前是否必须清空一些状态。

标签: javascriptreactjsmongooseredux

解决方案


您是否尝试将 Content-Type 标头设置为“multipart/form-data”。毕竟您不是在发布 JSON,而是在发布 FormData 对象。

如果您是从邮递员发送它作为 json 对象,那么您也应该在代码中进行。但是,如果您从邮递员那里将其作为 FormData 发送,那么您应该更改标题。


推荐阅读