dannyelo
dannyelo4mo ago

External API fetch advise

Hello, I'm using Convex folder for all my data-access layer now. I need to fetch some data from other APIs and they don't interact with Convex. I'm using Next.js. Is there any downside if I use an action in a 'use node' file for fetching data, or is better to use a next.js route instead?
4 Replies
jamwt
jamwt4mo ago
yeah, no problem using an action with use node if you need to even if you're not using the database, that's fine
dannyelo
dannyeloOP4mo ago
Thank you @jamwt!
jamwt
jamwt4mo ago
yw!
dannyelo
dannyeloOP4mo ago
Hey Jamie, I'm experiencing a weird pattern here.
export const getFacturapiOrganization = action({
args: {
organizationId: v.id('organizations'),
},
handler: async (ctx, args) => {
// ... some other logic

const facturapi = await getFacturapiUserInstance()
return facturapi.organizations.retrieve(
organization.facturapiOrganizationId,
)
},
})
export const getFacturapiOrganization = action({
args: {
organizationId: v.id('organizations'),
},
handler: async (ctx, args) => {
// ... some other logic

const facturapi = await getFacturapiUserInstance()
return facturapi.organizations.retrieve(
organization.facturapiOrganizationId,
)
},
})
This is my action function But then in the frontend, I'm trying this...
const getFacturapiOrganization = useAction(
api.organizations_node.getFacturapiOrganization,
)
useEffect(() => {
getFacturapiOrganization({
organizationId: currentOrganization?.id as Id<'organizations'>,
})
}, [currentOrganization, getFacturapiOrganization])
const getFacturapiOrganization = useAction(
api.organizations_node.getFacturapiOrganization,
)
useEffect(() => {
getFacturapiOrganization({
organizationId: currentOrganization?.id as Id<'organizations'>,
})
}, [currentOrganization, getFacturapiOrganization])
And then the only thing left is saving the response in state Is there a better way? @jamwt This is working, but I think there is too much code.
const [facturapiOrganization, setFacturapiOrganization] = useState<any>()

const getFacturapiOrganization = useAction(
api.organizations_node.getFacturapiOrganization,
)
useEffect(() => {
const fetchFacturapiOrganization = async () => {
const organization = await getFacturapiOrganization({
organizationId: currentOrganization?.id as Id<'organizations'>,
})
setFacturapiOrganization(organization)
}
fetchFacturapiOrganization()
}, [currentOrganization, getFacturapiOrganization])
const [facturapiOrganization, setFacturapiOrganization] = useState<any>()

const getFacturapiOrganization = useAction(
api.organizations_node.getFacturapiOrganization,
)
useEffect(() => {
const fetchFacturapiOrganization = async () => {
const organization = await getFacturapiOrganization({
organizationId: currentOrganization?.id as Id<'organizations'>,
})
setFacturapiOrganization(organization)
}
fetchFacturapiOrganization()
}, [currentOrganization, getFacturapiOrganization])

Did you find this page helpful?