我的获取路线有什么问题?当我尝试运行Get路线时,我会收到401状态代码

发布于 2025-02-02 01:36:10 字数 1078 浏览 3 评论 0原文

我的获取路线是返回401个未经授权的状态代码,但不确定,但一切都在邮递员中起作用,因此我假设这是在我的反应中孤立的,而不是我的API问题。使用携带者令牌进行AUTH,并在需要时可以提供更多的auth代码。使用mern堆栈

我客户端路由的API文件中获取路由 -

export const getOneApplication = (user, applicationId) => {
    return axios({
        url: `${apiUrl}/application/${applicationId}`,
        method: 'GET',
        headers: {
            Authorization: `Token token=${user.token}`
        }
    })
} 

从app.js的

<Route
    path='/application/:id'
    element={
    <ShowApplication user={user} msgAlert={msgAlert}/>}
/>

从后端获取路由

router.get('/application/:id', requireToken, (req, res, next) => {
    // req.params.id will be set based on the `:id` in the route
    Application.findById(req.params.id)
        .then(handle404)
        // if `findById` is succesful, respond with 200 and "application" JSON
        .then((application) => res.status(200).json({ application: application.toObject() }))
        // if an error occurs, pass it to the handler
        .catch(next)
})

my get route is returning a 401 unauthorized status code and undefined but everything works in postman so I'm assuming that this is isolated in my react and not a problem with my api. Using a bearer token for auth and can provide more of the auth code if needed. using mern stack

get route in the api file of my client-

export const getOneApplication = (user, applicationId) => {
    return axios({
        url: `${apiUrl}/application/${applicationId}`,
        method: 'GET',
        headers: {
            Authorization: `Token token=${user.token}`
        }
    })
} 

route from app.js-

<Route
    path='/application/:id'
    element={
    <ShowApplication user={user} msgAlert={msgAlert}/>}
/>

get route from backend api-

router.get('/application/:id', requireToken, (req, res, next) => {
    // req.params.id will be set based on the `:id` in the route
    Application.findById(req.params.id)
        .then(handle404)
        // if `findById` is succesful, respond with 200 and "application" JSON
        .then((application) => res.status(200).json({ application: application.toObject() }))
        // if an error occurs, pass it to the handler
        .catch(next)
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2025-02-09 01:36:10

答案最终是我没有将用户传递到组件中的使用效应,在运行路线时,这会导致我丢失令牌并获取401状态代码

    const [application, setApplication] = useState(null)
    const {user} = props
    const { id } = useParams()
    console.log('id in showApplication', id)
    // empty dependency array in useEffect to act like component did mount
    useEffect(() => {
        getOneApplication(user, id)
            .then(res => {
                console.log('this is the res', res)
                setApplication(res.data.application)})
            .catch(() => {
                console.log('show failed')
            })
    }, [])

The answer ended up being that I was not passing the user into the useEffect in the component, which when running the route caused me to lose the token and get the 401 status code

    const [application, setApplication] = useState(null)
    const {user} = props
    const { id } = useParams()
    console.log('id in showApplication', id)
    // empty dependency array in useEffect to act like component did mount
    useEffect(() => {
        getOneApplication(user, id)
            .then(res => {
                console.log('this is the res', res)
                setApplication(res.data.application)})
            .catch(() => {
                console.log('show failed')
            })
    }, [])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文