Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot convert undefined or null to object at Function.entries (<anonymous>)

I’m trying to pull data from the console on one page, written in json and then transfer it to another using this script, but I’m having problems.
Help solve the problem)

JS code

chosen = JSON.parse(sessionStorage.getItem('chosen'))
console.log(chosen)
let s = "";
let sum = 0.0;

for (let [key, value] of Object.entries(chosen)) {
    s += `<p>${key} (${value['value']}): \u20ac ${value['price']}</p>`
    sum += parseFloat(value['price'])
}
s += `<p>Total: ${sum}</p>`

document.getElementById("components").innerHTML += s;

Error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

order.js:6 Uncaught TypeError: Cannot convert undefined or null to object
    at Function.entries (<anonymous>)
    at order.js:6

>Solution :

Can you define your chosen variable correctly and try again?

This chosen = JSON.parse(sessionStorage.getItem('chosen')) should be like this => const chosen = JSON.parse(sessionStorage.getItem('chosen'))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading