Unable to fetch Orders with account_Id
Hi,
I have been tryin to fetch orders related to a users account.
I know swell provides a backend api to fetch order by Id. Each orders also have account id's associated with them, I have been trying to fetch specific orders based on the account_id associated with them since there is no other way to get the order_id except from fetching the swell.get('/order') which fetch all the order...
Am i doing this wrong or is there a way to get this data from my app.
async function getOrder(accountId: string) { try { const res = await swellNode.get('/orders', { params: { account_id: accountId, }, }); return res.results; } catch (error) { console.error('Failed to fetch orders:', error); throw error; } }
i wrote this simple function to fetch orders based on the users account, but this will continue to return an [ ] array
Perhaps some one could help point out how i could achieve this
I was able to do this by manually filtering the results, but its alot of messy code and was wondering if there is a better way to achieve this.
Also note: I am working on a next 13 app router project
import 'server-only'; import Hero from '@/components/hero'; import swellNode from '@/utils/swell/swellNode';
async function getOrder(accountId: string) { try { const res = await swellNode.get('/orders'); return res.results; } catch (error) { console.error('Failed to fetch orders:', error); throw error; } }
export default async function Home() { const accountId = '655d038da557950012c4f54dde'; // Replace with the actual account ID try { const allOrders = await getOrder();
// Filter orders based on the account ID
const ordersForAccount = allOrders.filter(order => order.account_id === accountId);
console.log(ordersForAccount);
// Render your component with filtered orders
return (
<div className=''>
<Hero />
<div className='flex'>
{/* Render your component with filtered orders */}
</div>
</div>
);
} catch (error:any) { // Handle errors, e.g., show an error message to the user console.error('Error:', error.message); return (
Error fetching orders. Please try again later.
@Daniel-Alamezie you can filter orders by account_id this way:
async function getOrders(accountId) {
const { results } = await swellNode.get('/orders', { account_id: accountId });
return results;
}
Your mistake was that you set filtering conditions in params, which is not needed.