trying to use fetch in a chain and getting the data to function using a callback












0















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);
}









share|improve this question

























  • 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


















0















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);
}









share|improve this question

























  • 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
















0












0








0








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);
}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 12:07









Sagar Zala

2,40151438




2,40151438










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 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



















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














1 Answer
1






active

oldest

votes


















0














You mixed up the call. This:



 displayproducts(getAllProducts)


Must be:



 getAllProducts(displayproducts);





share|improve this answer


























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














You mixed up the call. This:



 displayproducts(getAllProducts)


Must be:



 getAllProducts(displayproducts);





share|improve this answer


























  • 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
















0














You mixed up the call. This:



 displayproducts(getAllProducts)


Must be:



 getAllProducts(displayproducts);





share|improve this answer


























  • 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














0












0








0







You mixed up the call. This:



 displayproducts(getAllProducts)


Must be:



 getAllProducts(displayproducts);





share|improve this answer















You mixed up the call. This:



 displayproducts(getAllProducts)


Must be:



 getAllProducts(displayproducts);






share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)