I'm trying to make an application to get the recipes from https://edamam.com and I'm using fetch and Request object. I need to make 3 request, and i thought that most beautiful way for do it is make an Object and a method that return the data in JSON. I declarated into constructor a variable called this.dataJson, and i want to save there the data in JSON from the response. For that purpose i use this. The problem is that i have a undefined variable.
.then( data => {this.dataJson=data;
console.log(data)} )
This is all my code.
class Recipe{
constructor(url){
this.url=url;
this.dataJson;
this.response;
}
getJson(){
var obj;
fetch(new Request(this.url,{method: 'GET'}))
.then( response => response.json())
.then( data => {this.dataJson=data;
console.log(data)} )
.catch( e => console.error( 'Something went wrong' ) );
}
getData(){
console.log("NO UNDFEIND"+this.dataJson);
}
}
const pa= new Recipe('https://api.edamam.com/search?...');
pa.getJson();
pa.getData();
I'm new studying OOP in JS and more new in Fetch requests... If you guys can help me... Thanks very much!