Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 无法获取用户id-找不到404用户_Node.js_Reactjs_Express_Mern - Fatal编程技术网

Node.js 无法获取用户id-找不到404用户

Node.js 无法获取用户id-找不到404用户,node.js,reactjs,express,mern,Node.js,Reactjs,Express,Mern,当我导航到用户/仪表板时,我应该在url的末尾看到用户id,但我看到的是http://localhost:3000/profile/$%7B_id%7D无论谁登录,我都会收到404错误的响应:错误:“未找到用户”。我不知道$%7B_id%7D来自哪里 我需要在代码中更改什么才能修复此问题并获得正确的用户id export const isAuthenticated = () => { if (typeof window == 'undefined') { retur

当我导航到用户/仪表板时,我应该在url的末尾看到用户id,但我看到的是
http://localhost:3000/profile/$%7B_id%7D
无论谁登录,我都会收到404错误的响应:
错误:“未找到用户”
。我不知道
$%7B_id%7D
来自哪里

我需要在代码中更改什么才能修复此问题并获得正确的用户id

export const isAuthenticated = () => {
    if (typeof window == 'undefined') {
        return false;
    }
    if (localStorage.getItem('jwt')) {
        return JSON.parse(localStorage.getItem('jwt'));
    } else {
        return false;
    }
};
apiUser.js

import { API } from "../config";

export const read = (userId, token) => {
    return fetch(`${API}/user/${userId}`, {
        method: "GET",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`
        }
    })
        .then(response => {
            return response.json();
        })
        .catch(err => console.log(err));
};

export const update = (userId, token, user) => {
    return fetch(`${API}/user/${userId}`, {
        method: "PUT",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`
        },
        body: JSON.stringify(user)
    })
        .then(response => {
            return response.json();
        })
        .catch(err => console.log(err));
};

export const updateUser = (user, next) => {
    if (typeof window !== "undefined") {
        if (localStorage.getItem("jwt")) {
            let auth = JSON.parse(localStorage.getItem("jwt"));
            auth.user = user;
            localStorage.setItem("jwt", JSON.stringify(auth));
            next();
        }
    }
};
UserDashboard.js

const Dashboard = () => {
  const {
    user: { _id, name, email, role }
  } = isAuthenticated();

  const userLinks = () => {
    return (
            <Link className="nav-link" to="/profile/${_id}">
              Update Profile
            </Link>
    );
  };
const仪表板=()=>{
常数{
用户:{u id,名称,电子邮件,角色}
}=未验证();
常量userLinks=()=>{
返回(
更新配置文件
);
};

问题在于此功能:

export const isAuthenticated = () => {
    if (typeof window == 'undefined') {
        return false;
    }
    if (localStorage.getItem('jwt')) {
        return JSON.parse(localStorage.getItem('jwt'));
    } else {
        return false;
    }
};
返回一个布尔值

但在这里:

  const {
    user: { _id, name, email, role }
  } = isAuthenticated();

您正试图从中提取
\u id
,但它不知道
\u id
是什么。因此,如果您要解构我在使用
\u id
时忘了使用模板字符串和
{}
这里:
to=“/profile/${u id}>”


更改为:
to={`/profile/${u id}`}>
\u id
正在工作。

您确定您的isAuthenticated()返回正确的\u id吗?请显示您的isAuthenticated()代码也是。谢谢你的评论。我已经用我的代码更新了问题。@Vipalpatili仍然没有看到
IsAuthentication
code@RahniSorry,我添加了错误的代码。我已经更新了它@RedBaron@Rahni检查答案谢谢你的回答@RedBaron,但我刚刚意识到我犯了一个愚蠢的错误。我犯了一个错误,因为我没有使用{}在这里使用_-id:
to=“/profile/${u-id}”>
时使用模板字符串。是的,这都是固定的。谢谢。@redbaronn不担心。请随意投票并添加您自己的答案,以便人们知道问题已经解决:)