我试图部署我的代码,但当运行构建时,在TS上我得到一个“对象”是类型未知的错误,我试图找出如何解决我的情况。
export default function Home() {
async function sendEmail(e: any) {
e.preventDefault();
const formData = {};
Array.from(e.currentTarget.elements).forEach(field => {
if ( !field.name ) return;
formData[field.name] = field.value;
});
await fetch('/api/email', {
method: 'POST',
body: JSON.stringify(formData)
});
document.getElementById('emailform').onreset();
}
return (
<form className={styles.submitformcont} method="post" onSubmit={sendEmail} id='emailform'>
<label className={styles.fullnamelbl} htmlFor="name">Name:</label>
<br/>
<input className={styles.fullnamebtn} type="text" id="name" placeholder='Name' name="name" />
<br/>
<br/>
<label className={styles.emaillbl} htmlFor="mail">E-mail:</label>
<br/>
<input className={styles.emailbtn}type="email" id="mail" name="email" placeholder="Enter your Email" required/>
<br/>
<br/>
<label className={styles.textlbl}htmlFor="msg">Message:</label>
<br/>
<textarea className={styles.textbtn} id="msg" name="Message" placeholder="Message"></textarea>
<br/>
<button className={styles.submitbtn}> Submit </button>
</form>
)}
'field'的类型为'unknown'. ts(18046)
1条答案
按热度按时间yrdbyhpb1#
e.target.elements
包含button
元素,您不希望收集button
元素的值。所以你应该检查field
的类型。仅当值为HTMLInputElement
或HTMLTextAreaElement
时才收集该值。stackblitz