我是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.
发布评论
评论(1)
我也遇到了同样的问题和此评论:帮助。
您访问
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 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 todata()
withdata.state === "ready"
or via the other means as described here: https://docs.solidjs.com/references/api-reference/basic-reactivity/createResource#version-140-and-laterI would do this inside
authUserResource()
, handle the 401 there and only use the resource/signal result for injecting the API data into the view.