0

I created a small api using nodejs and express.

Basically i create a post request with an image url. The api then makes a request to the image url using axios and response in array buffer format. Then the api then returns the data to client. But the problem is the api returns undefined or null data to the client before axios sends the request and gets the response. The code is given below.

function comic_page(page_link){
    axios.get(page_link,{responseType: 'arraybuffer'}).then(resp => {
      console.log(resp.data)
      return resp.data;
      
  });
  }

function decryptImage(comic) {
    var burden =comic_page(comic.page_url)
    return burden

}
app.post('/test', function(request, response){
    var kiss=decryptImage(request.body)
//   console.log(request.body);      // your JSON
  //  response.send(kiss);    // echo the result back
});

var server = app.listen(8080, function () {
    var host = server.address().address
    var port = server.address().port
    console.log("Example app listening at http://%s:%s", host, port)
 })
  • its because its async. return the axios.get to return the promise. skip the decryptImage function because its doing thing and in your test route just do `comic_page(req.body).then((result) => {//rest of your endpoint logic here})` – richardsefton Jul 01 '21 at 15:26
  • i am not able to understand brother, could you rewrite and show me code snipped if possible? @richardsefton – Shaun Menezes Jul 01 '21 at 15:45
  • honestly your best bet is to research promises. Youtube is your friend there – richardsefton Jul 01 '21 at 17:21

0 Answers0