开发者问题收集

我无法在输入框中输入任何信息

2021-03-24
30

我无法在输入中输入任何信息。输入部分是空白的,但当我输入时什么也没有出现。它们之前是可以工作的,而且我没有对任何代码进行任何更改。当我能够输入数据时,我无法将数据发布到后端。我一直收到错误“POST 500(内部服务器错误)”。

const Properties = props =>{
    const [property,setProperty] = useState({
        street : '',
        town : '',
        area : '',
        PostCode : '',
        NoBeds : '',
        NoBath : '',
        NoLivingRooms : '',
    })
    const [properties,setProperties] = useState ([]);
    const [message,setMessage] = useState(null);
    const authContext = useContext(AuthContext);

    useEffect(()=>{
        PropertyService.getProperties().then(data =>{
            setProperties(data.properties);
        });
    },[]);

    const onSubmit = e =>{
        e.preventDefault();
        PropertyService.postProperty(property).then(data =>{
            const {message } = data;
            resetForm();
            if(!message.msgError){
                PropertyService.getProperties().then(getData =>{
                    setProperties(getData.properties);
                    setMessage(message);
                });
            }
            else if(message.msgBody === "UnAuthorized"){
                setMessage(message);
                authContext.setUser({username : ""});
                authContext.setIsAuthenticated(false);
            }
            else{
                setMessage(message);
            }
        },[]);
    }
     
    const onChange = e =>{
        setProperty({...property,[e.target.street] : e.target.value})
    }

    const resetForm = ()=>{
        setProperty({
        street : '',
        town : '',
        area : '',
        PostCode : '',
        NoBeds : '',
        NoBath : '',
        NoLivingRooms : '',
        });
    }

    return(
        <div>
            <ul className="">
                {
                    properties.map((property) =>{
                        return <PropertyItem key={property._id} 
            property={property} 
            street={property.street}
            town={property.town}
            area={property.area}
            PostCode={property.PostCode}
            NoBeds={property.NoBeds}
            NoBath={property.NoBath}
            NoLivingRooms={property.NoLivingRooms}
            />
                    },[])
                }
            </ul>
            <br/>
            <form onSubmit={onSubmit}>
                <label htmlFor="street">Street Name</label>
                <input type="text" 
                       name="Street name" 
                       value={property.street}
                       onChange={onChange}
                       className=""
                       placeholder="street name"/>
                <label htmlFor="town">Town</label>
                <input type="text" 
                       name="town" 
                       value={property.town}
                       onChange={onChange}
                       className=""
                       placeholder="town"/>
                <label htmlFor="area">Area</label>
                <input type="text" 
                       name="area" 
                        value={property.area}
                       onChange={onChange}
                       className=""
                       placeholder="area"/>
                <label htmlFor="postcode">Postcode</label>
                <input type="text" 
                       name="PostCode" 
                       value={property.PostCode}
                       onChange={onChange}
                       className=""
                       placeholder="Postcode"/>
                <label htmlFor="NoBeds">Beds</label>
                <input type="number" 
                       name="NoBeds" 
                       value={property.NoBeds}
                       onChange={onChange}
                       className=""
                       placeholder="Number of beds"/>
                <label htmlFor="NoBath">Bathrooms</label>
                <input type="number" 
                       name="NoBath" 
                        value={property.NoBath}
                       onChange={onChange}
                       className=""
                       placeholder="Number of bathrooms"/>
                <label htmlFor="NoLivingRooms">Living Rroom</label>
                <input type="number" 
                       name="NoLivingRooms" 
                        value={property.NoLivingRooms}
                       onChange={onChange}
                       className=""
                       placeholder="Number of living rooms"/>
                <button className="" 
                        type="submit">Submit</button>
            </form>
            {message ? <Message message={message}/> : null}
        </div>
    );

}
The error appears to be related to the second line of the code below. I can post using POSTMAN but when it comes to trying in the front end, It's just errors.

postProperty : property => {
        return fetch('/user/property', {
            method : "post",
            body : JSON.stringify(property),
            headers: {
                'Content-Type' : 'application/json'
            }
        }).then(res => res.json())
        .then(data => data);
    }
}
1个回答

我发现您的代码中有几个错误。主要是 onChange。正确的 onChange 函数将是:

const onChange = e =>{
    setProperty({ ...property, [e.target.name]: e.target.value });
}

Street Name 输入中,您将其命名为“Street Name”,但在访问值时,您仅使用“Street”作为属性对象中的键。

Remember whatever you set your name of the input, the value of that input will be stored keeping the name as the key.

在代码中进行一些类似的更改后,您就可以开始了。您可以在 此处 查看完整代码。

Divyesh Puri
2021-03-24