trying to use fetch in a chain and getting the data to function using a callback
Hi I have been trying to fetch from two API's using a fetch chain, the fetch works and the data displays correctly using console.log inside the getallproducts function but when I try to make a callback towards the displayproducts function using the data, the data is nowhere to be seen. I do not know exactly what I am doing wrong or if im using the wrong method for this but I have been trying multiple methods of fetch. promise.all seemed to be a solution but that didn't work either.
showProductsPage function
function showProductsPage() {
var page = document.getElementById('products-page');
hideAllPages();
//getAllProducts();
displayproducts(getAllProducts);
page.style.display = 'block';
}
getallproducts function
function getAllProducts(callback){
var producten;
var authors;
fetch(window.location.href+"api/products")
.then(response => response.json())
.then(data => {
producten = data.products;
console.log(data);
callback(data)
return fetch(window.location.href+"api/authors")
}).then(response => response.json())
.then(data => {
authors = data.authors;
callback(data)
})
}
display products function
function displayproducts(data) {
console.log(data.products);
for (var i = 0; i < data.products.length; i++) {
var card = document.createElement("div");
var img = document.createElement("img");
var name = document.createElement("BUTTON");
var author = document.createElement("p");
var published = document.createElement("p");
var price = document.createElement("p");
var cart = document.createElement("BUTTON");
document.getElementById("products-page").appendChild(card);
//
card.appendChild(img);
card.appendChild(name);
card.appendChild(author);
card.appendChild(published);
card.appendChild(price);
card.appendChild(cart);
//
card.setAttribute("class", "book-card");
img.setAttribute("class", "product-img");
name.setAttribute("class", "book-title");
author.setAttribute("class", "author");
published.setAttribute("class", "published");
price.setAttribute("class", "price");
cart.setAttribute("class", "add-to-cart");
//
cart.innerHTML = "Add to Cart";
name.innerHTML = data.products.title;
price.innerHTML = data.products.price;
};
//console.log(producten[1].price);
}
javascript api fetch
add a comment |
Hi I have been trying to fetch from two API's using a fetch chain, the fetch works and the data displays correctly using console.log inside the getallproducts function but when I try to make a callback towards the displayproducts function using the data, the data is nowhere to be seen. I do not know exactly what I am doing wrong or if im using the wrong method for this but I have been trying multiple methods of fetch. promise.all seemed to be a solution but that didn't work either.
showProductsPage function
function showProductsPage() {
var page = document.getElementById('products-page');
hideAllPages();
//getAllProducts();
displayproducts(getAllProducts);
page.style.display = 'block';
}
getallproducts function
function getAllProducts(callback){
var producten;
var authors;
fetch(window.location.href+"api/products")
.then(response => response.json())
.then(data => {
producten = data.products;
console.log(data);
callback(data)
return fetch(window.location.href+"api/authors")
}).then(response => response.json())
.then(data => {
authors = data.authors;
callback(data)
})
}
display products function
function displayproducts(data) {
console.log(data.products);
for (var i = 0; i < data.products.length; i++) {
var card = document.createElement("div");
var img = document.createElement("img");
var name = document.createElement("BUTTON");
var author = document.createElement("p");
var published = document.createElement("p");
var price = document.createElement("p");
var cart = document.createElement("BUTTON");
document.getElementById("products-page").appendChild(card);
//
card.appendChild(img);
card.appendChild(name);
card.appendChild(author);
card.appendChild(published);
card.appendChild(price);
card.appendChild(cart);
//
card.setAttribute("class", "book-card");
img.setAttribute("class", "product-img");
name.setAttribute("class", "book-title");
author.setAttribute("class", "author");
published.setAttribute("class", "published");
price.setAttribute("class", "price");
cart.setAttribute("class", "add-to-cart");
//
cart.innerHTML = "Add to Cart";
name.innerHTML = data.products.title;
price.innerHTML = data.products.price;
};
//console.log(producten[1].price);
}
javascript api fetch
Why are you fetching the authors?displayproducts
doesn't seem to use them. Also, inside your for loop, you needdata.products[i].title
and the like. Also, here's how to grab multiple pieces of data: pastebin.com/raw/x51h0fYB
– Chris G
Nov 21 '18 at 12:10
add a comment |
Hi I have been trying to fetch from two API's using a fetch chain, the fetch works and the data displays correctly using console.log inside the getallproducts function but when I try to make a callback towards the displayproducts function using the data, the data is nowhere to be seen. I do not know exactly what I am doing wrong or if im using the wrong method for this but I have been trying multiple methods of fetch. promise.all seemed to be a solution but that didn't work either.
showProductsPage function
function showProductsPage() {
var page = document.getElementById('products-page');
hideAllPages();
//getAllProducts();
displayproducts(getAllProducts);
page.style.display = 'block';
}
getallproducts function
function getAllProducts(callback){
var producten;
var authors;
fetch(window.location.href+"api/products")
.then(response => response.json())
.then(data => {
producten = data.products;
console.log(data);
callback(data)
return fetch(window.location.href+"api/authors")
}).then(response => response.json())
.then(data => {
authors = data.authors;
callback(data)
})
}
display products function
function displayproducts(data) {
console.log(data.products);
for (var i = 0; i < data.products.length; i++) {
var card = document.createElement("div");
var img = document.createElement("img");
var name = document.createElement("BUTTON");
var author = document.createElement("p");
var published = document.createElement("p");
var price = document.createElement("p");
var cart = document.createElement("BUTTON");
document.getElementById("products-page").appendChild(card);
//
card.appendChild(img);
card.appendChild(name);
card.appendChild(author);
card.appendChild(published);
card.appendChild(price);
card.appendChild(cart);
//
card.setAttribute("class", "book-card");
img.setAttribute("class", "product-img");
name.setAttribute("class", "book-title");
author.setAttribute("class", "author");
published.setAttribute("class", "published");
price.setAttribute("class", "price");
cart.setAttribute("class", "add-to-cart");
//
cart.innerHTML = "Add to Cart";
name.innerHTML = data.products.title;
price.innerHTML = data.products.price;
};
//console.log(producten[1].price);
}
javascript api fetch
Hi I have been trying to fetch from two API's using a fetch chain, the fetch works and the data displays correctly using console.log inside the getallproducts function but when I try to make a callback towards the displayproducts function using the data, the data is nowhere to be seen. I do not know exactly what I am doing wrong or if im using the wrong method for this but I have been trying multiple methods of fetch. promise.all seemed to be a solution but that didn't work either.
showProductsPage function
function showProductsPage() {
var page = document.getElementById('products-page');
hideAllPages();
//getAllProducts();
displayproducts(getAllProducts);
page.style.display = 'block';
}
getallproducts function
function getAllProducts(callback){
var producten;
var authors;
fetch(window.location.href+"api/products")
.then(response => response.json())
.then(data => {
producten = data.products;
console.log(data);
callback(data)
return fetch(window.location.href+"api/authors")
}).then(response => response.json())
.then(data => {
authors = data.authors;
callback(data)
})
}
display products function
function displayproducts(data) {
console.log(data.products);
for (var i = 0; i < data.products.length; i++) {
var card = document.createElement("div");
var img = document.createElement("img");
var name = document.createElement("BUTTON");
var author = document.createElement("p");
var published = document.createElement("p");
var price = document.createElement("p");
var cart = document.createElement("BUTTON");
document.getElementById("products-page").appendChild(card);
//
card.appendChild(img);
card.appendChild(name);
card.appendChild(author);
card.appendChild(published);
card.appendChild(price);
card.appendChild(cart);
//
card.setAttribute("class", "book-card");
img.setAttribute("class", "product-img");
name.setAttribute("class", "book-title");
author.setAttribute("class", "author");
published.setAttribute("class", "published");
price.setAttribute("class", "price");
cart.setAttribute("class", "add-to-cart");
//
cart.innerHTML = "Add to Cart";
name.innerHTML = data.products.title;
price.innerHTML = data.products.price;
};
//console.log(producten[1].price);
}
javascript api fetch
javascript api fetch
edited Nov 21 '18 at 12:07
Sagar Zala
2,40141337
2,40141337
asked Nov 21 '18 at 12:00
Alex PenningssAlex Penningss
13
13
Why are you fetching the authors?displayproducts
doesn't seem to use them. Also, inside your for loop, you needdata.products[i].title
and the like. Also, here's how to grab multiple pieces of data: pastebin.com/raw/x51h0fYB
– Chris G
Nov 21 '18 at 12:10
add a comment |
Why are you fetching the authors?displayproducts
doesn't seem to use them. Also, inside your for loop, you needdata.products[i].title
and the like. Also, here's how to grab multiple pieces of data: pastebin.com/raw/x51h0fYB
– Chris G
Nov 21 '18 at 12:10
Why are you fetching the authors?
displayproducts
doesn't seem to use them. Also, inside your for loop, you need data.products[i].title
and the like. Also, here's how to grab multiple pieces of data: pastebin.com/raw/x51h0fYB– Chris G
Nov 21 '18 at 12:10
Why are you fetching the authors?
displayproducts
doesn't seem to use them. Also, inside your for loop, you need data.products[i].title
and the like. Also, here's how to grab multiple pieces of data: pastebin.com/raw/x51h0fYB– Chris G
Nov 21 '18 at 12:10
add a comment |
1 Answer
1
active
oldest
votes
You mixed up the call. This:
displayproducts(getAllProducts)
Must be:
getAllProducts(displayproducts);
works now....cant believe it was such a simple mistake, been working on this for a couple days, used a lot of methods but was just this. thanks!
– Alex Penningss
Nov 21 '18 at 12:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411600%2ftrying-to-use-fetch-in-a-chain-and-getting-the-data-to-function-using-a-callback%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You mixed up the call. This:
displayproducts(getAllProducts)
Must be:
getAllProducts(displayproducts);
works now....cant believe it was such a simple mistake, been working on this for a couple days, used a lot of methods but was just this. thanks!
– Alex Penningss
Nov 21 '18 at 12:11
add a comment |
You mixed up the call. This:
displayproducts(getAllProducts)
Must be:
getAllProducts(displayproducts);
works now....cant believe it was such a simple mistake, been working on this for a couple days, used a lot of methods but was just this. thanks!
– Alex Penningss
Nov 21 '18 at 12:11
add a comment |
You mixed up the call. This:
displayproducts(getAllProducts)
Must be:
getAllProducts(displayproducts);
You mixed up the call. This:
displayproducts(getAllProducts)
Must be:
getAllProducts(displayproducts);
answered Nov 21 '18 at 12:08
community wiki
Jonas Wilms
works now....cant believe it was such a simple mistake, been working on this for a couple days, used a lot of methods but was just this. thanks!
– Alex Penningss
Nov 21 '18 at 12:11
add a comment |
works now....cant believe it was such a simple mistake, been working on this for a couple days, used a lot of methods but was just this. thanks!
– Alex Penningss
Nov 21 '18 at 12:11
works now....cant believe it was such a simple mistake, been working on this for a couple days, used a lot of methods but was just this. thanks!
– Alex Penningss
Nov 21 '18 at 12:11
works now....cant believe it was such a simple mistake, been working on this for a couple days, used a lot of methods but was just this. thanks!
– Alex Penningss
Nov 21 '18 at 12:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411600%2ftrying-to-use-fetch-in-a-chain-and-getting-the-data-to-function-using-a-callback%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why are you fetching the authors?
displayproducts
doesn't seem to use them. Also, inside your for loop, you needdata.products[i].title
and the like. Also, here's how to grab multiple pieces of data: pastebin.com/raw/x51h0fYB– Chris G
Nov 21 '18 at 12:10