0

In my React component this.login() isn't executing. I believe this is because an await function changes the context of this, but I'm not sure how to fix it.

           await this.props
                .SignupUser({
                    variables: {
                        email: this.state.email,
                        password: this.state.password,
                        name: this.state.name,
                    },
                })
                .then(() => {
                    this.login();
                })
                .catch(error => {
                    this.setState({ wait: false });
                    const errorMessage = error.graphQLErrors[0].functionError;
                    this.setState({ error: errorMessage });
                });
1
  • is there any error in API call.Did you checked in catch Commented Apr 6, 2018 at 10:01

1 Answer 1

1

Remove await keyword, It should work.

Alternatively, we need to implement in a different way

The function must add async keyword in the function declaration for below code

 await this.props
    .SignupUser({
        variables: {
            email: this.state.email,
            password: this.state.password,
            name: this.state.name,
        },
    })
    .then(() => {
        this.login();
    })
    .catch(error => {
        this.setState({ wait: false });
        const errorMessage = error.graphQLErrors[0].functionError;
        this.setState({ error: errorMessage });
    });

(or)

    try {
    const user = await this.props
        .SignupUser({
            variables: {
                email: this.state.email,
                password: this.state.password,
                name: this.state.name,
            },
        });
    this.login();
} catch (e) {
    this.setState({ wait: false });
    const errorMessage = error.graphQLErrors[0].functionError;
    this.setState({ error: errorMessage });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.