简体   繁体   English

REACT-FORM-HOOK 类型错误:无法读取未定义的属性“类型”

[英]REACT-FORM-HOOK TypeError: Cannot read property 'type' of undefined

I'm trying to use a specific string as validation in my form, and I get this error:我试图在我的表单中使用特定字符串作为验证,但出现此错误:

TypeError: Cannot read property 'type' of undefined the logic is the admins have a special 'magicword' that they only know and it allows them to register to the system as a simple way to filter out non-admins to register TypeError: Cannot read property 'type' of undefined逻辑是管理员有一个他们只知道的特殊“魔法词”,它允许他们注册到系统作为过滤掉非管理员注册的简单方法

I'm attaching the error and a sandbox link, thanks in advance!我附上了错误和沙盒链接,在此先感谢!

  49 | <div>
  50 |     <label htmlFor="magicword_field">Your Magic Word</label>
> 51 |     <input type="text" id="magicword_field"
  52 |     ref={
  53 |         register({
  54 |             required: true,

https://codesandbox.io/s/exciting-bogdan-0z1bu?file=/src/App.js https://codesandbox.io/s/exciting-bogdan-0z1bu?file=/src/App.js

import React from "react";
import axios from 'axios';
import {useForm}  from "react-hook-form";
import './AdminSignup.css'

const AdminSignup = ({ setLoggedUser})  => {

    const { register, handleSubmit , errors , watch} = useForm();

        const onSubmit = (data) => {
            console.log(data)
            axios({
                method:'POST',
                url: 'http://127.0.0.1:5000/signup',
                data: data
            })
            setLoggedUser(true)
            .then(res => console.log(res))
            .catch(err => console.error(err))
    }

    const passWord = watch('password')

    return (
        <div className = "wrap form-page">
            <div className='nes-container with-title'>
            <h2 className='title'>
                Register Admin
            </h2>
            <br/>
                <form onSubmit={handleSubmit(onSubmit)}>
                    <div>
                        <label htmlFor="name_field">Your name</label>
                        <input type="text" id="name_field" ref={register({required: '😱 forgot your name, old friend? 😱' })} name='name'/>
                        {errors.name && <p className='formError'>{errors.name.message}</p>}
                    </div>
                    <div>
                        <label htmlFor="email_field">Your email</label>
                        <input type="email" id="email_field" ref={register({ required: '📨 we prefer this over magic scrolls 📨', pattern:  /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/})} name='email'/>
                        {errors.email && <p className='formError'>{errors.email.message}</p>}
                    </div>
                    <div>
                        <label htmlFor="password_field">Your Password</label>
                        <input type="password" id="password_field" ref={register({ required:'👹 no password? are you evena wizard?? 👹'})} name='password'/>
                        {errors.password && <p className='formError'>{errors.password.message}</p>}
                    </div>
                    {
                        passWord &&
                        <div>
                            <label htmlFor="magicword_field">Your Magic Word</label>
                            <input type="text" id="magicword_field"
                            ref={
                                register({
                                    required: true,
                                    validate:  value => value === 'nerd'
                                },
                                )} name='magicword'/>
                            {errors.magicword.type === 'required' && <p className='formError'>
                                {'🔮 time to use it, just like they told you 🔮'}</p>}
                            {errors.magicword.type === 'validate' && <p className='formError'>
                                {'time to stop lying to yourself, buddy!'}</p>}
                        </div>

                    }
                    <br/>
                    <button className='b btn'>Register</button>
                </form>
            </div>
        </div>
    )

}
export default AdminSignup

I think you need to apply some defensive programming step before you access errors.magicword.type .我认为您需要在访问errors.magicword.type之前应用一些防御性编程步骤。 Just use !!只需使用!! check whether errors.magicword.type is undefined or not.检查 errors.magicword.type 是否undefined

          {(!!errors.magicword.type) && (errors.magicword.type=== "required" && (
            <p className="formError">
              {"🔮 time to use it, just like they told you 🔮"}
            </p>
          ))}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的resolveModel react-redux-form的属性“ 0” - TypeError: Cannot read property '0' of undefined resolveModel react-redux-form 反应来自 Redux 的工作。 TypeError:无法读取未定义的属性“类型” - React work from Redux . TypeError: Cannot read property 'type' of undefined Uncaught TypeError:无法读取未定义的React / Redux的属性&#39;type&#39; - Uncaught TypeError: Cannot read property 'type' of undefined React/Redux 反应类型错误:无法读取未定义的属性“type1” - react TypeError: Cannot read property 'type1' of undefined 类型错误:无法读取反应 js 中未定义的属性“类型” - TypeError: Cannot read property 'type' of undefined in react js 如何解决 TypeError:使用 useMutation react hook 时无法读取 react js 中未定义的属性“then” - How to solve TypeError: Cannot read property 'then' of undefined in react js when in use of useMutation react hook 未被捕获的TypeError:无法读取未定义的属性“ form” - Uncaught TypeError: Cannot read property 'form' of undefined 反应TypeError:无法读取未定义的属性&#39;bind&#39; - React TypeError: Cannot read property 'bind' of undefined 反应-TypeError:无法读取未定义的属性&#39;imageurl&#39; - React - TypeError: Cannot read property 'imageurl' of undefined TypeError:无法读取react js中未定义的属性“then” - TypeError: Cannot read property 'then' of undefined in react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM