-
First of all, thank you for this great project! I currently get the following error message with Angular:
This is my code: public getTaskById(
id: string,
): Result<QueryObserverResult<Task | undefined, Error>> {
return this.#query({
queryKey: ['tasks', id],
queryFn: () => this.tasksService.getTaskById(id),
});
} As you can see, I am searching an entity I understand the error message. However, I wonder what the best practices are to implement this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Not definitely an expert but I'm also curious to know the answer. Anyway in my opinion:
{
queryKey: ['tasks', id]
queryFn: () => this.taskService.getTaskById(id).then((data) => {
if (data) {
throw new MyCustomError()
}
return data
}),
retry? // ... handle logic for MyCustomError
throwOnError? // handle logic for MyCustomError
} |
Beta Was this translation helpful? Give feedback.
-
if you want the query go to into error state when the item does not exist, you can check and throw a custom error as the answer by @riccardoperra shows. if you're fine with having no data for a successfully resolved query, I'd just transform to
of course you could also change |
Beta Was this translation helpful? Give feedback.
if you want the query go to into error state when the item does not exist, you can check and throw a custom error as the answer by @riccardoperra shows.
if you're fine with having no data for a successfully resolved query, I'd just transform to
null
because that is allowed:of course you could also change
getTaskById
to already returnnull
.