Hi guys im trying to create a product using nodejs shopify library and get its variant id but the return value is undefined.
import {Product} from '@shopify/shopify-api/dist/rest-resources/2022-04/index.js';
const test_session = await Shopify.Utils.loadCurrentSession(request, response);
const product = new Product({session: test_session});
product.title = "Burton Custom Freestyle 151";
product.body_html = "**Good snowboard!**";
product.vendor = "Burton";
product.product_type = "Snowboard";
product.published = false;
const outdata = await product.save({});
The product is created but outdata is undefined;
Thanks in advance for any help.
Found it! i used save and update like below
import {Product} from '@shopify/shopify-api/dist/rest-resources/2022-04/index.js';
const test_session = await Shopify.Utils.loadCurrentSession(request, response);
const product = new Product({session: test_session});
product.title = "Burton Custom Freestyle 151";
product.body_html = "**Good snowboard!**";
product.vendor = "Burton";
product.product_type = "Snowboard";
product.published = false;
await product.saveAndUpdate({});
res.status(200).send(product);
3 Likes
Hi AnthonyCy,
I have use both cases but itâs returning undefind again. Here is my Code.
const { Product } = await import(
@shopify/shopify-api/dist/rest-resources/2022-10/index.js
);
const session = await Shopify.Utils.loadCurrentSession(
req,
res,
app.get(âuse-online-tokensâ)
);
// console.log(âhitetsâ,session)
const product = new Product({ session });
product.title = âBurton Custom Freestyle 151â;
product.body_html = âGood snowboard!â;
product.vendor = âBurtonâ;
product.product_type = âSnowboardâ;
product.tags = [
âBarnes & Nobleâ,
âBig Airâ,
âJohnâs Favâ
];
// await product.save()
await product.saveAndUpdate({})
.then((rData)=>console.log(ârestDataâ,rData))
.catch((err)=>console.log(âerrâ,err));
I was facing the same issue and the promise return undefined but later I figured out that the product object itself is updated with response data. Try returning the âproductâ object or try console.log(product) and you will get response data.