Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to fetch Orders with account_Id #29

Open
Daniel-Alamezie opened this issue Nov 22, 2023 · 2 comments
Open

Unable to fetch Orders with account_Id #29

Daniel-Alamezie opened this issue Nov 22, 2023 · 2 comments

Comments

@Daniel-Alamezie
Copy link

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

@Daniel-Alamezie
Copy link
Author

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.



);
}
}

@awwit
Copy link
Contributor

awwit commented Jan 5, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants