am currently building a recipe app, I have a list of recipes pulling from an API. For this to work I do a http get request as below. I want to pull the {{id}} from the JSON and place it in the url where (id) is.
I’m not sure if you have already attempted to pass the data from one page to another, some more code would be needed as I’m not sure how this whole thing structured.
Why is id in parenthesis? When using concatenation, you don’t need parenthesis. The app is looking in a variable that doesn’t exist for the value so it’s getting undefined. If Id has the right value, getting rid of the parenthesis should work.
Thanks for the reply, I have attempted to use this but kinda failed to be honest. I took away the parenthesis and I still have an undefined value. I understand the value that gets put into the URL is this.id, but I cant work out how to check if I have this ID or not. Further assistance would be great.
So I have a listing page which lists out recipes in certain categories which the html is (this is just a normal http get request which pulls from a provider)
export class Category1Page {
private rate = 2.5;
public api: any;
detailsPage = DetailsPage;
constructor(public navCtrl: NavController, public navParams: NavParams, public apiAuthentication: ApiAuthentication) {
this.loadRecipes();
}
loadRecipes(){
this.apiAuthentication.loadCategory1()
.then(data => {
this.api = data;
});
}
}
I then has a details page which is the recipe in its singular form (listing out ingredients, method… etc) which I currently test with a single recipe until this issue is solved.
The fundamental principle of asynchronous programming in JavaScript is that when you create a future (either Promise or Observable) you also provide some chain code in the form of a then or subscribe block.
Only within that chain block can you rely on the value resolved from the future. You can stash it in an object property, but absolutely anywhere that object property is referenced (both in templates and backing code) outside of the chain block must be able to cope with the situation that it hasn’t been set yet. So if you’re going to kick off further HTTP requests (such as by calling loadRecipes) you must do it from within a then or subscribe that is providing the id. You cannot rely on the object property being set anywhere else.