getProduct(productId: number): Promise<Product | null>¶
The getProduct is an asynchronous method that allows to fetch a single product by its id.
Input parameters¶
productId - a number representing the product id.
Returned value¶
A returned value has a type of Promise<Product | null>. You can read more about Product model here.
Example¶
In this example we make a ProductFetcherApi call to get a product with id 1.
useStorefront(async (storefront) => {
let productFetcherApi = await storefront.getApi('ProductFetcherApi');
if (!productFetcherApi) {
const featureSystemApi = this.getApiSync('FeatureSystemApi');
await featureSystemApi.registerDynamic('ProductFetcher');
productFetcherApi = await storefront.getApi('ProductFetcherApi');
}
const product = await productFetcherApi.getProduct(1);
console.log(product);
});