Access Prop Type передан в функцию MERN Stack - PullRequest
0 голосов
/ 22 апреля 2020

В настоящее время я передаю свою переменную resetPassword через функцию updateUserPassword, используя типы проп. Затем функция проверяет ответное сообщение из маршрута API, который я настроил, и изменяет логическое значение resetPassword.isMatched.

Проблема заключается в том, что я не могу получить доступ к обновленному значению isMatched при отправке , Как вывести истинное или ложное значение переменной isMatched?

Функция updateUserPassword успешно устанавливает для resetPassword.isMatched значение true или false в зависимости от res.data.msg, к которому у меня просто нет доступа мой начальный компонент.

//*** Onsubmit that passes the prop types to updateUserPassword ***//
onSubmit = e => {
   e.preventDefault();

   const resetPassword = {
     email: this.username,
     password: this.state.password,
     password2: this.state.password2,
     isMatched: false
   };

   this.props.updateUserPassword(resetPassword);

};


//*** Passing prop types to as function ***//

ResetPassword.propTypes = {
  updateUserPassword: PropTypes.func.isRequired,
  auth: PropTypes.object.isRequired,
  errors: PropTypes.object.isRequired
};


//*** @route POST api/users/resetPassword ***//

router.post("/resetpassword", (req, res) => {

  User.findOne({ email: req.body.email }).then(user => {

   if (!user) {

     return res.status(400).json({ email: "User does not exist" });

   } else {

     const updPassword = req.body.password;

     // Check password
     bcrypt.compare(updPassword, user.password).then(isMatch => {

       if (isMatch) {
         res.status(200).send({
           msg: 'passwords match',
           isMatched: true
         })
       } else {
         // Hash password before saving in database
         bcrypt.genSalt(10, (err, salt) => {
           bcrypt.hash(resetPassword, salt, (err, hash) => {
             if (err) throw err;
             user.password = hash;
             user
               .save()
               .then(user => res.json(user))
               .catch(err => console.log(err));
           });
         });
       }
      });
    }
  });

});


//*** Reset User Password function that accepts resetPassword ***//

export const updateUserPassword = (resetPassword) => dispatch => {
axios
.post("/api/users/resetpassword", resetPassword)
.then(res => {
  if (res.data.msg === 'passwords match') {

    resetPassword.isMatched = true;

  } else {

    resetPassword.isMatched = false;

  }
})
.catch(err =>
  dispatch({
    type: GET_ERRORS,
    payload: err.response.data
  })
);
};
...