I’m using The Shopify Buy plugin in my react-native application for guest checkout
But I’m facing the issue (see the image below)
I also refered https://github.com/Shopify/js-buy-sdk/issues/518 for solution but I dont have this type of problem in my code.
Please any help is appreciated.
My Code
import Client from 'shopify-buy';
const client = Client.buildClient({
storefrontAccessToken: my_token,
domain: my_domain
});
export default class App extends Component {
componentWillMount() {
client.checkout.create().then((res) => {
this.setState({
checkout: res,
});
});
}
_handleAddToBagBtn = (variantID) => {
const checkoutId = this.state.checkout.id;
const lineItemsToAdd = [{'variantID': variantID, quantity: 1}];
client.checkout.addLineItems(checkoutId, lineItemsToAdd).then((checkout) => {
console.warn(checkout.lineItems);
});
}
}
render() {
return (
<TouchableOpacity onPress={() => this._handleAddToBagBtn(variantID)} >Click Here</TouchableOpacity>
);
}
2
Answers
What seems to me is that
Client.buildClient()
is returning a promise. You should do something like this:You can check if you are getting a valid response or an error and go ahead accordingly.
I was facing this problem today using the Storefont API, but solved it as follows.
Instead of using the
variantId
numeric value directly, you need to use a Base64 encoded string of thegraphql_api_id
property which is associated with the variant.Unencoded, these look like this:
gid://shopify/ProductVariant/12434084921391
. You can get these from product objects, but you can also create them directly from your numericvariantId
.In your code, change this function:
}
Note I also changed
variantID
tovariantId
as this is the identifier used by Shopify, notvariantID
.In my case, this got rid of the error and returned a GraphQL model. Hope that helps.