简体   繁体   English

我收到错误“无法读取未定义的属性“用户名””

[英]I am getting the error "cannot read property "username" of undefined"

signup.js注册.js


const Signup = () => {
    const [username, Username] = useState("");
    const [password, Password] = useState("");
    

    const Post = () => {
        fetch("/signup", {
            method: "post",
            headers: {
                "Content-Type":"application/json",
                "Accept":"application/json",
            },
            body: JSON.stringify({
                username: "",
                password: "",
            })
        })
        .then(response => response.json())
        .then(data => {
            console.log(data)
        })
    }

    return (
        <div>
            <input type="text" placeholder="username" value={username} onChange={ (e) => Username(e.target.value) }/>
            <br/>
            <input type="text" placeholder="password" value={password} onChange={ (e) => Password(e.target.value) }/>
            <br/>
            <button id="submit_button" onClick={ () => Post() }>Sign up</button>
        </div>
    )
}

function from auth.js from backend来自后端的 auth.js 函数

router.post("/signup", (request, response) => {
    const username = request.body.username;
    const password = request.body.password;

    // If missing a field: error
    if (!username || !password) {
        response.status(422).json({ error: "Please add all fields" })
    }

    // Checks if there already exists an account with that username
    user.findOne({ username: username })
    .then ((saved_user) => {
        if (saved_user) {
            return response.status(422).json({ error: "There already exists an account with that username"})
        }
    })

    // Creates a new user and saves the account
    const user = new user({ username, password })
    user.save()
    .then(user => {
        response.json({ message: "Account Saved Sucessfully"})
    })
    .catch(error => {
        console.log(error)
    })
})

Does anyone know what's wrong?有谁知道出了什么问题? It seems like the json object I'm posting from signup.js as an undefined body for some reason.由于某种原因,我从 signup.js 发布的 json 对象似乎是未定义的主体。 I already declared username as a constant earlier in the function though.不过,我已经在函数的前面将 username 声明为常量。 IDK how to fix that. IDK如何解决这个问题。

Solved.解决了。 Forgot to add App.use(express.json());忘记添加App.use(express.json()); before routes.在路线之前。

You are not giving the username and password in the body您没有在正文中提供用户名和密码

body: JSON.stringify({
                username: "",
                password: "",
            }).
const Signup = () => {
    const [username, setUsername] = useState("");
    const [password, setPassword] = useState("");
    

    const Post = () => {
        fetch("/signup", {
            method: "POST",
            headers: {
                "Content-Type":"application/json",
                "Accept":"application/json",
            },
            body: JSON.stringify({
                username: username,
                password: password,
            })
        })
        .then(response => response.json())
        .then(data => {
            console.log(data)
        })
    }

    return (
        <div>
            <input type="text" placeholder="username" value={username} onChange={ (e) => setUsername(e.target.value) }/>
            <br/>
            <input type="text" placeholder="password" value={password} onChange={ (e) => setPassword(e.target.value) }/>
            <br/>
            <button id="submit_button" onClick={ () => Post() }>Sign up</button>
        </div>
    )
}

Try to use like this , maybe it can work.尝试这样使用,也许它可以工作。

暂无
暂无

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

相关问题 每当我尝试访问 record.username 时,我都会收到“TypeError: Cannot read property 'username' of undefined” - I am getting "TypeError: Cannot read property 'username' of undefined" whenever I try to access record.username 我收到一个错误:无法读取未定义的属性“推送” - I am getting an error: Cannot read property 'push' of undefined 我收到错误 Typerror 无法读取未定义的属性“执行” - I am getting an error Typerror Cannot read property 'execute' of undefined 我在控制台中收到此错误:无法读取未定义的属性“ some” - I am getting this error in the console : Cannot read property 'some' of undefined 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 我收到以下错误:错误TypeError:无法读取未定义的属性&#39;invalid&#39; - I am getting the following error: ERROR TypeError: Cannot read property 'invalid' of undefined 无法读取未定义错误的属性“用户名” - Cannot read property 'username' of undefined error 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM