简体   繁体   English

react-TypeError:无法读取未定义的属性'trim'

[英]react - TypeError: Cannot read property 'trim' of undefined

I'm not sure what I'm doing wrong here. 我不确定我在做什么错。

class Login extends Component {

    render() {
        const username = "me@domain.com  ";
        console.log(username)
        const password = this.refs.password;
        const creds = {
            username: username.value.trim(),
            password: password.value.trim()
        };
        const errorMessage  = this.props;

        return ( ... ) } }

console shows "me@domain.com" yet it's erroring on username: username.value.trim() 控制台显示“ me@domain.com”,但username: username.value.trim()错误

  11 | console.log(username)
  12 | const password = this.refs.password;
  13 | const creds = {
> 14 |     username: username.value.trim(),
  15 |     password: password.value.trim()
  16 | };
  17 | const errorMessage  = this.props;
username.value.trim()

username itself a String, you need not to get value out of it. username本身是一个字符串,您无需从中获取价值。 Just trim. 修剪一下

const creds = {
        username: username.trim(),
        password: password.value.trim()
    };

username is not an object but a string string and hence you would write username不是object而是字符串,因此您将编写

const username = "me@domain.com  ";
const creds = {
        username: username.trim(),
        password: password.trim()
    };

Also you shouldn't use string refs as they are deprecated. 同样,您不应该使用string refs因为它们已被弃用。

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

相关问题 react-native - 类型错误:无法读取未定义的属性“修剪” - react-native - TypeError: Cannot read property 'trim' of undefined 未捕获的类型错误:无法读取未定义的属性“修剪” - Uncaught TypeError: Cannot read property 'trim' of undefined 未捕获的类型错误:无法读取 Jquery 中未定义的属性“修剪” - Uncaught TypeError: Cannot read property 'trim' of undefined in Jquery 用 FIREBASE 表达我得到 TypeError: Cannot read property 'trim' of undefined - Express with FIREBASE I get TypeError: Cannot read property 'trim' of undefined TypeError:无法读取节点应用程序中未定义的属性“修剪” - TypeError: Cannot read property 'trim' of undefined in Node application TypeError:无法读取未定义节点 Js firebase 函数的属性“修剪” - TypeError: Cannot read property 'trim' of undefined Node Js firebase functions React - TypeError:无法读取未定义的属性“setState” - React - TypeError: Cannot read property 'setState' of undefined 反应:TypeError:无法读取未定义的属性“ Item” - React : TypeError: Cannot read property 'Item' of undefined 类型错误:无法在反应中读取未定义的属性“toLowerCase” - TypeError: Cannot read property 'toLowerCase' of undefined in react 反应类型错误:无法读取未定义的属性“文本” - React TypeError:Cannot read property 'text' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM