Shopify / storefront-api-examples

Example custom storefront applications built on Shopify's Storefront API
https://help.shopify.com/api/storefront-api
MIT License
1.12k stars 329 forks source link

UnhandledPromiseRejectionWarning errors when using Fetch Product example #92

Closed brettbullock-zz closed 5 years ago

brettbullock-zz commented 5 years ago

When implementing the "Fetching Products" example (specifically, fetching a single product) from the documentation on JS Buy SDK - https://shopify.github.io/js-buy-sdk/ - the product information is not return in the console due to the following errors:

(node:19627) UnhandledPromiseRejectionWarning: [object Array] (node:19627) 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: 2) (node:19627) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

brettbullock-zz commented 5 years ago

Shopify-specific code that is causing error

`//Shopify JS Buy SDK declarations import Client from 'shopify-buy';

//using require as recommended in node-fetch package docs global.fetch = require('node-fetch');

const client = Client.buildClient({ domain: 'brett-demo-2.myshopify.com', storefrontAccessToken: '7ed0eea0b9115d3f5e9c0c6bc41d3a12' });

// Fetch a single product by ID const productId = '1565306421270';

client.product.fetch(productId).then((product) => { // Do something with the product console.log(product); });`

rebeccajfriedman commented 5 years ago
client.product.fetch(productId)
    .then((product) => {
        // Do something with the product
        console.log(product);
    })
    .catch((reason) => {
       console.log('Handle rejected promise ('+reason+') here.');
    });

Look at the example here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Advanced_Example