POST http://localhost:3000/api/v1/register 500 (内部服务器错误)

5 浏览
0 Comments

POST http://localhost:3000/api/v1/register 500 (内部服务器错误)

当我在Postman中不包含Cloudinary时,我的代码可以正常运行。但是当我在注册路由中包含Cloudinary以添加照片到账户时,我遇到了这个错误。

userAction.jsx

export const register = (userData) => async (dispatch) => {
    try {
      dispatch({ type: REGISTER_USER_REQUEST });
      const config = { headers: { "Content-Type": "multipart/form-data" } };
      const { data } = await axios.post(`/api/v1/register`, userData, config);
      dispatch({ type: REGISTER_USER_SUCCESS, payload: data.user });
    } catch (error) {
      dispatch({
        type: REGISTER_USER_FAIL,
        payload: error.response.data.message,
      });
    }
  };

LoginSignup.jsx

import React, {Fragment,useRef,useState, useEffect} from "react";
import "./LoginSignUp.css";
import Loader from "../layout/Loader/Loader";
import { Link, useNavigate } from "react-router-dom";
import LockOpenIcon from '@mui/icons-material/LockOpen';
import EmailIcon from '@mui/icons-material/Email';
import PersonIcon from '@mui/icons-material/Person';
import img from "../../images/Profile.png";
import {useSelector, useDispatch} from "react-redux";
import {clearErrors, login , register} from "../../actions/userAction.jsx";
import {useAlert} from "react-alert";
export default function LoginSignUp(){
    const dispatch = useDispatch();
    const alert = useAlert();
    const navigate = useNavigate();
    const {error,loading,isAuthenticated} = useSelector((state)=>state.user);
    const loginTab=useRef(null);
    const registerTab = useRef(null);
    const switcherTab = useRef(null);
    const [loginEmail,setLoginEmail] = useState("");
    const [loginPassword,setLoginPassword] = useState("");
    const [user,setUser] = useState({
        name:"",
        email:"",
        password:"",
    });
    const {name,email,password} = user;
    const [avatar, setAvatar] = useState(img);
    const [avatarPreview, setAvatarPreview] = useState(img);
    function loginSubmit(e){
        e.preventDefault();
        dispatch((login(loginEmail,loginPassword)));
    }
    function registerSubmit(e){
        e.preventDefault();
        const myForm = new FormData();
        myForm.set("name",name);
        myForm.set("email",email);
        myForm.set("password",password);
        myForm.set("avatar",avatar);
        dispatch(register(myForm));
    }
    function registerDataChange(e){
        if(e.target.name==="avatar"){
            const reader = new FileReader();
            reader.onload = ()=>{
                if(reader.readyState===2){
                    setAvatarPreview(reader.result);
                    setAvatar(reader.result);
                }
            };
            reader.readAsDataURL(e.target.files[0]);
        } else {
            setUser({...user, [e.target.name]:e.target.value});
        }
    }
    useEffect(()=>{
        if(error){
            alert.error(error);
            dispatch(clearErrors());
        }
        if(isAuthenticated){
            navigate(`/account`);
        }
    },[dispatch,error,alert,isAuthenticated,navigate]);
    const switchTabs = (e, tab) => {
        if (tab === "login") {
          switcherTab.current.classList.add("shiftToNeutral");
          switcherTab.current.classList.remove("shiftToRight");
          registerTab.current.classList.remove("shiftToNeutralForm");
          loginTab.current.classList.remove("shiftToLeft");
        }
        if (tab === "register") {
          switcherTab.current.classList.add("shiftToRight");
          switcherTab.current.classList.remove("shiftToNeutral");
          registerTab.current.classList.add("shiftToNeutralForm");
          loginTab.current.classList.add("shiftToLeft");
        }
      };
    return(
        
            {loading ?  : (
                
                                    

switchTabs(e,"login")}>Login

switchTabs(e,"register")}>Register

Avatar Preview
)}
); }

userController.jsx

exports.RegisterUser = catchAsyncErrors( async (req,res,next)=>{
    // const myCloud = await cloudinary.v2.uploader.upload(req.body.avatar,{
    //   folder: "avatars",
    //   width: 150,
    //   crop:"scale",
    // });
    const {name,email,password} = req.body;
    const user = await User.create({
        name,
        email,
        password,
        avatar: {
            public_id:"myCloud.public_id",
            url: "myCloud.secure_url",
        }
    });
    sendToken(user,201,res);
});

  1. 我在Cloudinary上有一个ID,并导入了它的Name、API_KEY和API_SECRET
  2. 后端代码在Postman上没有Cloudinary时运行良好
0