In my node application that interacts with the shopify api
I’m upgrading from using:
const shopifyAPI = require(‘shopify-node-api’);
to using:
import ‘@shopify/shopify-api/adapters/node’;
and I have this method that requires updating:
export function getOrderByName(orderName) {
return new Promise((resolve, reject) => {
Shopify.get(‘/admin/api/2023-04/orders.json’, { name: orderName }, (err, data) => {
if (err) return reject(err);
if (!data || !data.orders || !data.orders.length) {
return reject(httpErrors(404, Order with name ${orderName} was not found.
));
}
return resolve(data.orders[0]);
});
});
}
where I can get an order from shopify according to the ‘name’ property instead of just the ShopifyId
I can’t find a new similar route according to the new API documentation that allows for me to do this. So currently as a work around, I’m getting orders in a series of batches and then searching through them for a matching name
export async function getOrderByName(orderName) {
try {
const getOrderPage = async (pageInfo) => {
const response = await shopify.rest.Order.all({
…pageInfo?.query,
session,
limit: 250,
});
const orders = response.data;
const order = orders.find((order) => order.name === orderName.toUpperCase());
if (order != null) {
return order;
}
if (response.pageInfo?.nextPage) {
return getOrderPage(response.pageInfo.nextPage);
} else {
throw httpErrors(404, Order with name ${orderName} was not found.
);
}
};
return await getOrderPage(null);
}catch (err){
debug(ERROR while fetching order by name: ${orderName}, Error: ${err}
);
throw httpErrors(404, Order with name ${orderName} was not found.
);
}
}
but this is a terrible work around since it can take way to long to look through batches trying to match a property.
Is there something else I can do to?