0

My find Ingredient function gets 1 argument which i pass to my GET api. When I only console log my result.body I get undifined.

So I looked it up and i need to use callback in my .end function. But when I do, it returns an error: Callback does not exist on type void

 findIngredient(searchTerm: HTMLInputElement)  {
       this.unirest.get('https://spoonacular-recipe-food-nutrition-
   v1.p.mashape.com/recipes/' +
'findByIngredients?fillIngredients=false&ingredients=' 
 + searchTerm.value + '&limitLicense=false&number=5&ranking=1')
.header('X-Mashape-Key', 'key1')
.header('X-Mashape-Host', 'host1')
.end(function (result) {

const data = result.body.data;
  if (!result.error && result.statusCode === 200) {
    callback(null, data);
  } else {
    console.log('Failed response', result.error)
      .callback(result.error, null);
  }
});

}

Stijn
  • 1
  • 2

1 Answers1

0

I guess you have a typo here

console.log('Failed response', result.error)
.callback(result.error, null);

remove the dot before .callback

console.log('Failed response', result.error);
callback(result.error, null);

The first one is trying to call the .callback function from the return of console.log, that returns nothing ('void').

Christian Benseler
  • 7,907
  • 8
  • 40
  • 71
  • Because you don't have a callback function passed as parameter. This is another issue. You have copied-pasted this piece of code from somewhere? Why do you need this callback? What you are trying to do? – Christian Benseler Oct 31 '17 at 13:40
  • I need this callback because if i try to log my result.body i get undifined. So i looked it up and found a solution on this item -> https://stackoverflow.com/questions/38008042/returned-unirest-response-in-node-js-is-undefined – Stijn Oct 31 '17 at 13:50