NodeJS mern栈前端实现忘记口令

oxf4rvwz  于 2022-11-29  发布在  Node.js
关注(0)|答案(1)|浏览(139)

我在mern堆栈中创建了一个忘记密码的端点。我在使用Thunder客户端发送忘记密码时为忘记密码创建了一个API,它将忘记密码链接发送到客户端以输入电子邮件ID,但我想在忘记密码前端页面中实现它。我尝试了,但它不起作用

This is my thunder client screenshot Api work perfect

API work screenshot
电子邮件接收屏幕截图click here to see image

This is my Reset.js frontend file
import React, { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { toast } from 'react-toastify';

const Reset = () => {
  const navigate = useNavigate()
  const [email, setEmail] = useState({email: ""})
  const postData = async (e) => {
    e.preventDefault();
    // eslint-disable-next-line
    // if (!/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email)) {
    //   toast.error("invalid email");
    //   return
    // }
      const response = await fetch("http://localhost:5000/api/auth/reset-password", {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        email
      })
    }).then(res => res.json())
      .then(data => {
        if (data.error) {
          toast.error("invalid data");
        }
        else {
          toast.success("valid email")
          navigate('./login')
        }
      }).catch(err => {
        console.log(err);
      })
  }

  return (
    <div>
      <form>
        <div className="container" style={{ marginTop: "4.8rem", marginRight: "0px" }}>
          <div className="col-md-6 col-lg-6 col-xl-4 offset-xl-1">
            <div className="form-outline mb-4">
              <label className="form-label" htmlFor="form3Example3"> Enter valid Email address</label>
              <input type="email" id="email" value={email} onChange={(e)=>setEmail(e.target.value)} name="email" className="form-control form-control-lg" required
                placeholder="Enter email address" style={{ backgroundColor: "#eaedf0" }} />
            </div>
            <div className="text-center text-lg-start mt-2">
              <button type="submit" onClick={() => postData()} className="btn btn-outline-warning btn-lg">send link</button>
            </div>
          </div>
        </div>
      </form>
    </div>
  )
}

export default Reset

任何帮助都将不胜感激

eagi6jfj

eagi6jfj1#

我认为可能存在两个问题:
1.您正在以strinigied格式发送主体。请尝试以json格式发送。
1.在Thunder客户端,我看到了3个头文件。默认情况下有2个头文件,所以我猜你一定添加了一个额外的头文件。检查是否需要添加它来获取。

相关问题