Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Promise { pending } Shopify JavaScript Buy SDK

Promise { pending } Shopify JavaScript Buy SDK

jeremyllee27
Visitor
1 0 0

I am using the Shopify SDK... I am attempting to extract the checkout id so I can use it globally or in middleware and share it with multiple pages. but I keep getting stuck in a Promise { pending }, which is the error. I was able to extract it before but it keeps creating checkout id's for every page. so I am attempting to isolate the checkout id creation so it will create only once and I  can share the checkout id between each page in the website.

 

const Client = require("shopify-buy");
const fetch = require("node-fetch");
global.fetch = fetch;
const client = Client.buildClient({
  storefrontAccessToken: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  domain: "swagunco.myshopify.com",
});
const createCheckoutId = async () => {
  let store = client.checkout.create().then((checkout) => {
    // Do something with the checkout
    return checkout.id;
  });
  return store;
};
console.log(createCheckoutId());

 

I am attempting to store the checkout id in a variable and add it to middleware and pass it as req.query.id. or in local storage but I need the checkout id to trigger only once. If there is a better way to do this I am all ears. thanks in advance.

Below is the example of how my code is structured I am using server side rendering.

https://github.com/Shopify/storefront-api-examples/tree/master/node-js-buy 

 

 

Reply 1 (1)

assafl
Shopify Partner
16 0 5

You can try using the await command, meaning this (note that I'm not fully familiar with the actual types used here but the concept should be enough)

app.get('/', async (req, res) => {
....
let checkout = await client.checkout.create();

Than checkout.id will be your data to use at the rest of the flow

 

The issue with the current code is that the function doesn't wait for the execution to finish and it ends without you getting what you need.

You want to make it execute at an order that will get your data from the async functions and allow you to use it as the code flows.

 

HTH