Next.js Discord

Discord Forum

Nextjs14 server render fetch issue

Answered
Crazy ant posted this in #help-forum
Open in Discord
Crazy antOP
Hello all , How can I fetch a server action in a if condition. For example I have an application where I need to assure that if customer data is available then fetch order. let customerOrder = {} if(customer){ const {order} = await fetchOrder({phone:customer.phone}) customerOrder = order } Now I will use customerOrder. And I want fetch will only work in case of customer data available. If customer data is not available, then customerOrder should be an empty object. Is there any correct way to implement this function.
Answered by Ray
i would do this
async function fetchOrder(customer=null) {
  if (!customer) return {}
  ...
}
View full answer

4 Replies

i would do this
async function fetchOrder(customer=null) {
  if (!customer) return {}
  ...
}
Answer
Crazy antOP
Thanks for the reply. can I do like this export default async function page({params}) { const {customer} = await fetchCustomer({id: params.id }) if(!customer) return {} const {order} = await fetchOrder({phone:customer.phone}) }
Crazy antOP
Hello @Ray, you are right. I got your point .Thank you so much for the help.