SK1dev
SK1dev

Reputation: 1139

Cannot get user id - 404 User not found

When I navigate to the user/dashboard, I should see the user id at the end of the url but instead I see http://localhost:3000/profile/$%7B_id%7D no matter who is signed in and I get a 404 error with the response: error: "User not found". I don't know where $%7B_id%7D is coming from.

What do I need to change in my code to fix this and get the correct user 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>
    );
  };

Upvotes: 1

Views: 1547

Answers (2)

SK1dev
SK1dev

Reputation: 1139

I forgot to use template strings and {} when using _id here: to="/profile/${_id}">

Changed to: to={`/profile/${_id}`}> and _id is working.

Upvotes: 0

Red Baron
Red Baron

Reputation: 7642

the problem is that this function:

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

returns a boolean

but here:

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

you are trying to abstract _id out of it and it doesn't know what _id is. so you need to make sure that it returns an object with these keys if you're going to destructure

Upvotes: 2

Related Questions