0

So here's my situation:

I upload a file and send it to an API then i get back a response with an object with some data in it. This is how i send it to the API:

apiRoute.post(async (req, res) => {
  try {
    const media_url = `${req.webhookBaseUrl}/media/${req.file.filename}`;
    const webhook_url = `${req.webhookBaseUrl}/api/job`;
   
    const job = await req.asyncClient.submitJobUrl(media_url, {
      callback_url: webhook_url,
    });

    res.json(job);

  } catch (error) {
    console.dir(`Error: ${error}`);
    res.status(500).json({ error });
  }
 
});

Then i get the response here on my Nextjs api route like this:

/api/job.js

export default async function handler(req, res) {
  try {
    
    const result = await req.body;
    
    res.status(200).json({ result });
    console.log("webhook received:", result);
 
  } catch (err) {
    res.status(500).send({ error: "failed to TRANSCRIBE data" });
  }
}

so i get the object just fine in the terminal:

enter image description here

...now in the front end i use SWR to get this data but it always returns and empty string:

const jobFetcher = async (...args) => {
  const response = await fetch(...args);
  const jobData = await response.json();
  
  console.log("JOB PREJ TE FETCHER", jobData);
  return jobData;
};

const { data: job, error: jobError } = useSWR(`/api/job`, jobFetcher);
  console.log("JOBBB", job);

enter image description here

What am i doing wrong here? Maybe i have to save the response to a storage or a state before i fetch it with SWR? If so, how to do it? Any clues? Thanks!

Ilir
  • 488
  • 5
  • 20
  • I suppose the first code is of an endpoint from expressjs? why don't you bring that code to `/api/job` and send the result directly to the client or call the first endpoint? – mocherfaoui Aug 30 '22 at 12:30
  • Is your API returning `Cache-Control` headers? Mine is returning `public,max-age=60` and I'm experiencing the same problem after those 60s. – Fábio Batista Jan 05 '23 at 18:37

1 Answers1

0

It seems that you forgot await

const jobFetcher = async (...args) => {
  const response = await fetch(...args);
  const jobData = await response.json();
  
  console.log("JOB PREJ TE FETCHER", jobData);
  return jobData;
};