SOLIDJS创建资源 - 未透露的Promise错误

发布于 2025-02-11 03:17:58 字数 940 浏览 0 评论 0 原文

我是SolidJ的新手,并且资源有一个奇怪的错误。这是我的代码:

export const authUserResource = createResource(
    () => axios.get<AuthUser>('/expense-tracker/api/oauth/user').then((res) => res.data)
);

export const MyComponent = () => {
    const [data] = authUserResource;
    createEffect(() => console.log('HasChecked', data.loading, data.error, data()));
    return (
        // ...
    )
}

API调用的目的是在当前身份验证的用户上检索数据,或者如果当前未对用户进行身份验证,则返回401。我的目的是根据用户的身份验证状态重定向。

好吧,API被调用,并且由于用户未经身份验证而返回401。所有这些都是期望的。没有行为的是SolidJS资源。

如果API返回401,我希望SolidJS资源数据能够满足这些条件:

data.loading === false
data.error instanceof Error
data() === undefined

相反,我发现它仍然处于这些条件下,基于Console.Log语句在上述代码示例中:

data.loading === true
data.error === undefined
data() === undefined

我也得到了一个”未接收到的Promise错误“浏览器JS控制台中的错误消息”。

我希望在这里弄清楚我做错了什么,以使资源正确处理错误,以便我可以在应用程序中优雅地处理此错误。

I'm new to SolidJS and I'm having a weird error with resources. Here is my code:

export const authUserResource = createResource(
    () => axios.get<AuthUser>('/expense-tracker/api/oauth/user').then((res) => res.data)
);

export const MyComponent = () => {
    const [data] = authUserResource;
    createEffect(() => console.log('HasChecked', data.loading, data.error, data()));
    return (
        // ...
    )
}

The intent of the API call is to retrieve data on the currently authenticated user, or to return a 401 if the user is not currently authenticated. My intent is to then redirect the user based on their authentication status.

Well, the API is called and it is returning a 401 because the user is not authenticated. All of this is expected. What is not behaving is the SolidJS resource.

If the API returns a 401, I would expect the SolidJS resource data to meet these conditions:

data.loading === false
data.error instanceof Error
data() === undefined

Instead, I am finding that it is still at these conditions, based on the console.log statement in the above code example:

data.loading === true
data.error === undefined
data() === undefined

I also get an "Uncaught Error in Promise" error message in the browser JS console.

I'm hoping to figure out what I am doing wrong here to make the resource correctly handle the error so that I can gracefully handle this in my application.

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

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

发布评论

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

评论(1

深海里的那抹蓝 2025-02-18 03:17:58

我在浏览器JS控制台中也会收到“未接收错误”错误消息。

我也遇到了同样的问题和此评论:帮助。

您访问 data(),但是当出现错误时不允许使用。因此,您必须使用 data() data.state ===“准备就绪” 或通过此处所述的其他方式来捍卫 data() ://docs.solidjs.com/references/api-references/basic-reactivitivitivitive/createResource#version-140-and-later“ rel =“ nofollow noreferrer”> https://docs.solidjs.coms.coms.com/references/references/references/api-参考/基本反应性/Createresource#版本140及载客

我的目的是根据用户的身份验证状态重定向。

我会在 authuserresource()中进行此操作,处理401,仅使用资源/信号结果将API数据注入视图中。

I also get an "Uncaught Error in Promise" error message in the browser JS console.

I also had the same issue and this comment: https://github.com/solidjs/solid/discussions/705#discussioncomment-2968407 helped.

You access data() but it is not allowed when there is an error. So you have to guard the call to data() with data.state === "ready" or via the other means as described here: https://docs.solidjs.com/references/api-reference/basic-reactivity/createResource#version-140-and-later

My intent is to then redirect the user based on their authentication status.

I would do this inside authUserResource(), handle the 401 there and only use the resource/signal result for injecting the API data into the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文