I am facing an issue with my GraphQL mutation while trying to charge the user using Stripe. The payment is successfully processed on the server side and reflects in the database, but I am having trouble getting the updated user data back on the client side. The problem seems to be that the .then() function is triggering prematurely, resulting in a null response instead of the expected edited user information. It appears to be firing before the stripe.charges operation completes on the server.
Server-side :
chargeUser(args) {
const { userId, membershipId, stripeToken } = args;
return User.findById(userId)
.then((user) => {
return Membership.findById(membershipId)
.then((membership) => {
return stripe.charges.create({
amount: membership.price * 100,
currency: 'chf',
source: stripeToken,
description: `${user.firstname} ${user.lastname}`
}, function(err) {
if (!err) {
user.membership = membershipId;
return user.save();
} else {
console.log(err)
}
});
})
});
}
Client-side :
this.props.mutate({
variables: {
membershipId: this.props.membershipId,
userId: global.userId,
stripeToken: token.tokenId
}
})
.then((res) => {
// Returning null before the stripe.charges has finished
console.log(res)
})