Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible unhandled promise rejection warning with useMutation

I'm getting an unhandled promise rejection error when I use useMutation with react native. Here's the code producing the issue:

const [createUser, { error, loading }] = useMutation(CREATE_USER_MUTATION);

Anytime my graphql server returns an error to the client I get an unhandled promise rejection error (screenshot below). I'm able to make it go away by adding an error handler like so, but it seems like a hack. Any thoughts? Am I doing something wrong or is this something that should be addressed by the apollo folks?

const [createUser, { error, loading }] = useMutation(CREATE_USER_MUTATION, {
  onError: () => {}
});

enter image description here

like image 626
coops22 Avatar asked Aug 10 '19 15:08

coops22


People also ask

Does useMutation return a promise?

A function that performs an asynchronous task and returns a promise.

What is the correct syntax of using the useMutation hook in react?

Apollo React Mutation Update We pass the update function as an option to useMutation . - const [addTodo] = useMutation(ADD_TODO); + const [addTodo] = useMutation(ADD_TODO, {update: updateCache}); We need to fetch the current list of todos from the cache.

What is this unhandledpromiserejectionwarning error?

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch (). (rejection id: 4) To say the least, I have created a helper function which looks something like this

What is unhandled promise rejection in JavaScript?

If you do not provide a rejection handler callback to the promise, and it fails, this is where the Javascript runtime gets upset and throws the “Unhandled Promise Rejection” error. If you don’t tell the promise how to handle the error if it rejects, the error doesn’t know where to go. The solution.

How to access the error when a promise is rejected?

The answer is similar to when the promise is resolved; you access the error by registering a handler/callback by passing it to the .catch method on the promise. Below is an example of how you can access the error when a promise is rejected.

How to register a rejection handler in a promise chain?

To recap, To register a rejection handler in a promise chain, you need to call the .catch method on the promise and pass it to your rejection handler. Finally, the Unhandled Promise Rejection error! Now that we have an idea of the different states of a promise, can you think of why the “Unhandled Promise Rejection” error may be occurring?


1 Answers

Your createUser mutation is a promise you should handle error inside try catch block, or in upper scope inside apollo-link-error onError method.

like image 156
Oleg Kupriianov Avatar answered Oct 15 '22 10:10

Oleg Kupriianov