Next.js Discord

Discord Forum

How to access the data in a response for further processing in my client component

Answered
Pacific sand lance posted this in #help-forum
Open in Discord
Pacific sand lanceOP
I have an API route that i would like to send over the response to my client for further processing

Here is the code :

import { Invoice as InvoiceClient, Xendit } from "xendit-node";
import { NextResponse } from "next/server";

export async function POST(req) {
    const secret = process.env.XENDIT_KEY
    const xenditClient = new Xendit({secretKey: secret})
    const { Invoice } = xenditClient
    
    const xenditInvoiceClient = new InvoiceClient({secretKey: secret})

    const userdata = await req.json()

    try {
        const response = await xenditInvoiceClient.createInvoice({
            data: userdata
        })
        return NextResponse.json({response}, {status: 200})
    } catch (error) {
        console.log('error')
        return NextResponse.json({error}, {status: 500})
    }
    
}
Answered by riský
looks good
View full answer

28 Replies

American black bear
you are sending response, so what's the issue?
@American black bear you are sending response, so what's the issue?
Pacific sand lanceOP
i want to kinda send the response to my client component, so i can use the data in the response for conditional stuff
well, how are you sending the request from client?
as if fetch request, you can make a useState and set that, and then show the message on client
@riský well, how are you sending the request from client?
Pacific sand lanceOP
wait
@riský well, how are you sending the request from client?
Pacific sand lanceOP
try {
            const data = {
                'externalId': randomID,
                'amount': price,
                'description': paketumrah,
                'customer': {
                    'givenNames': namalengkap,
                    'email': email,
                    'mobileNumber': notelponhp,
                }
            }
            await fetch('/api/invoicemaker', {
                method: 'POST',
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(data),
            }) .then((response) => {
                if (!response.ok) {
                    throw new Error('Network response was not ok')
                }
                return response.json()
            }) .then((data) => {
                console.log(data)
                statusinv = data.status
                linkinvoice = data.invoiceUrl
            })
        } catch (error) {
            console.log(error)
        }
you could see that i tried to access the data from the response as
data.status
since the json response have a "status" in it
yeash, so you should be able to use useState and set the message insite this
Pacific sand lanceOP
so my code is correct? could you please elaborate on that further
show your whole file so i can see what you are actually using here
Pacific sand lanceOP
ah it's kinda long
but i'll just display what i'm primarily working on
like i more want to know more about statusinv and if it is a useState and you should be using the actuall way of setting
@riský show your whole file so i can see what you are actually using here
Pacific sand lanceOP
const handleSubmit = async (event) => {
        event.preventDefault()
        setSubmitting(true)

        let statusinv
        let linkinvoice

        try {
            const data = {
bunch of data
                           }
            }
            await fetch('/api/invoicemaker', {
                method: 'POST',
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(data),
            }) .then((response) => {
                if (!response.ok) {
                    throw new Error('Network response was not ok')
                }
                return response.json()
            }) .then((data) => {
                console.log(data)
                statusinv = data.status
                linkinvoice = data.invoiceUrl
            })
        } catch (error) {
            console.log(error)
        }

        if (statusinv == 'PENDING') {
            try {
                const userbody = { 
                    bunch of data 
                }
                    
                await fetch('/api/submitdata', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(userbody)
                })
    
                setSubmitting(false)
    
            } catch (error) {
                console.error(error)
                setSubmitfail(true)
                setSubmitted(false)
                }
            setSubmitted(true)

        }
  
    }
looks good
Answer
so do something like setSubmitfail but for the data (and then show that data with conditional or smth on client UI)
Pacific sand lanceOP
so the code is good? but the database isn't sending over
i mean idk what the problem you are having ngl
@Pacific sand lance so the code is good? but the database isn't sending over
as in, if you log the output of data, it isn't what you expected?
Pacific sand lanceOP
it just that the conditionals doesn't work
the api is working completely fine tho
Pacific sand lanceOP
ah the code is completely fine
just a tiny bit mistake
i fixed it!
i forgot how json works lmao
thank u for your time
ahh nice