Hey am trying to show some quotes using my JSON file. But it shows undefined. Can anyone check the below html and JavaScript.
My Javascript
const resultEl = document.querySelector('.allquotes');
const pageSize = document.querySelector('select[name="page-size"]');
const pageCurr = document.querySelector('input[name="page-curr"]')
const resultCount = document.querySelector('.result-count')
const pageNoCurr = document.querySelector('.page-no-curr');
const pageNoCount = document.querySelector('.page-no-count')
const btnFirst = document.querySelector('.page-btn-first');
const btnPrev = document.querySelector('.page-btn-prev');
const btnNext = document.querySelector('.page-btn-next');
const btnLast = document.querySelector('.page-btn-last');
let results = [];
const getResultCount = () => results.length;
const getPageSize = () => +pageSize.value;
const getCurrPage = () => +pageCurr.value;
const getPageCount = () => Math.ceil(getResultCount() / getPageSize());
const pageResponse = (records, pageSize, page) =>
(start => records.slice(start, Math.min(records.length, start + pageSize)))
(pageSize * (page - 1));
const main = async() => {
btnFirst.addEventListener('click', navFirst);
btnPrev.addEventListener('click', navPrev);
btnNext.addEventListener('click', navNext);
btnLast.addEventListener('click', navLast);
pageSize.addEventListener('change', changeCount);
results = await retrieveAllQuotes();
updatePager(results);
redraw();
};
const redraw = () => {
resultEl.innerHTML = '';
const paged = pageResponse(results, getPageSize(), getCurrPage());
const contents = document.createElement('div');
contents.innerHTML = paged.map(record => `<div class='latestatus'><p class='copytxt'>${record.quotes}</p><div> <button class="copystatus btn">Copy</button></div></div>`).join('');
resultEl.append(contents);
};
const navFirst = (e) => {
pageNoCurr.textContent = 1;
pageCurr.value = 1;
redraw();
}
const navPrev = (e) => {
const pages = getPageCount();
const curr = getCurrPage();
const prevPage = curr > 1 ? curr - 1 : curr;
pageCurr.value = prevPage;
pageNoCurr.textContent = prevPage;
redraw();
}
const navNext = (e) => {
const pages = getPageCount();
const curr = getCurrPage();
const nextPage = curr < pages ? curr + 1 : curr;
pageCurr.value = nextPage;
pageNoCurr.textContent = nextPage;
redraw();
}
const navLast = (e) => {
pageNoCurr.textContent = getPageCount();
pageCurr.value = getPageCount();
redraw();
}
const changeCount = () => {
updatePager();
redraw();
};
const updatePager = () => {
const count = getPageCount();
const curr = getCurrPage();
pageCurr.value = curr > count ? 1 : curr;
pageNoCurr.textContent = curr > count ? 1 : curr;
pageNoCount.textContent = count;
resultCount.textContent = getResultCount();
};
const retrieveAllQuotes = async function() {
// here we are making a network call to your api
const response = await fetch('/stat.json');
// then converting it to json instead of a readable stream
const data = await response.json();
// finally go over the array and return new object with renamed key
const results = data.map(val => ({quotes: val.status}));
return results;
}
main();
My Html
<div class="allquotes"></div>
<div class="pagable-status">
<label>Page <span class="page-no-curr">1</span> of <span class="page-no-count">1</span></label>
<div class="pagable-actions">
<button class="page-btn-first">≪</button>
<button class="page-btn-prev"><</button>
<input type="number" name="page-curr" min="1" value="1" />
<button class="page-btn-next">></button>
<button class="page-btn-last">≫</button>
<select name="page-size">
<option>5</option>
<option>10</option>
<option>20</option>
</select>
</div>
<label>(<span class="result-count"></span> items)</label>
</div>
I said that I use a JSON file. You can notice it on the javascript. There is a file name called stat.json. Location in my javascript
const retrieveAllQuotes = async function() {
// here we are making a network call to your api
const response = await fetch('/stat.json');
The content of stat.json is
[
{
"quotes":"Do what is right not what is easy."
},
{
"quotes":"Hey there, Whatsapp is using me."
},
{
"quotes":"Too Busy"
},
{
"quotes":"Only you can give me that feeling."
},
{
"quotes":"I had a horribly busy day in converting oxygen into carbon dioxide"
},
{
"quotes":"Be yourself; everyone else is already taken"
},
{
"quotes":"Your attitude may hurt me, But main can Kill You!!"
},
{
"quotes":"Love is Blind, be careful."
},
{
"quotes":"'SUCCESS' is depend on U."
},
{
"quotes":"If you love someone set it free."
},
{
"quotes":"Love is sweet, When it's new. But it is sweeter when it's true."
},
{
"quotes":"Where ther is love, there is life."
},
{
"quotes":"Not always 'Available' try your luck.."
},
{
"quotes":"I am not changed it's just I grew up and you should try too."
},
{
"quotes":"The biggest slap to your enimies is your success."
},
{
"quotes":"Born to express, not to impress."
},
{
"quotes":"When nothing goes right! go left."
},
{
"quotes":"I allow myself to be badass confident in all that I do."
},
{
"quotes":"Sometimes you succeed and other time you learn."
},
{
"quotes":"A true friend sees the first tear, catches the second and stops the third."
},
{
"quotes":"We carry our childhood with us!!"
},
{
"quotes":"Childhood is the most beautiful of all lige's season!!"
}
]
And these quotes want to displayed in a <p class="copytxt"> element created by Javascript. Location in my javascript
contents.innerHTML = paged.map(record => `<div class='latestatus'><p class='copytxt'>${record.quotes}</p><div> <button class="copystatus btn">Copy</button></div></div>`).join('');
resultEl.append(contents);
};
If I run this code it shows all the quotes undefined. PLEASE if anyone have time run this above code and test it twice.
[at the beginning. I guess this is a copying error since you're not getting a JSON parsing error.statuskey. They all have only aquoteskey. Your map isn't going to match any propertyval.statusshould beval.quotes.