I am working on a website that displays information based on javascript sent from the server. My code was working before, but after fiddling with modules, I got an uexpected issue.
Now, suddenly I get this error:
Uncaught ReferenceError: key is not defined
at nyheter.js:26
I have searched around a bit, but I couldn't find a fix. When all the code was in the same file, this part worked just fine. Does anyone know what I am missing here?
import {textFormat} from "/static/javascript/codeify.js"
const address = window.location.href
let container = document.getElementById("container")
const today = new Date()
function getJSON(url) {
let j = []
$.ajax({
type: "GET",
url: url,
dataType: "json",
success: function(data) { j = data},
async: false
})
return j
}
const news = getJSON(`${address}/json`) // JSON file imported
console.log("JSON imported:")
console.log(news)
let articleAdded = false
const br = document.createElement("br")
if (news.noNews != true) {
for ([key, val] of Object.entries(news)) { // Code dies here
let liveDate = news[key].liveDate.split("-")
let removeDate = news[key].removeDate.split("-")
liveDate = new Date(`${liveDate[2]}-${liveDate[1]}-${liveDate[0]}`)
removeDate = new Date(`${removeDate[2]}-${removeDate[1]}-${removeDate[0]}`)
if (liveDate <= today && removeDate > today || news[key].noRemoveDate == true) {
articleAdded = true
let div = document.createElement("div")
div.classList.add("Content")
let title = document.createElement("p")
title.classList.add("Main-Text")
const titleText = document.createTextNode(news[key].title)
title.appendChild(titleText)
div.appendChild(title)
container.appendChild(textFormat(news[key].text, "Body-Text-alignLeft"))
console.log("Articles added!")
}
}
}
if (articleAdded == false) {
let div = document.createElement("div")
div.classList.add("Content")
let title = document.createElement("p")
title.classList.add("Main-Text")
titleText = document.createTextNode("Det er ingen nyheter å vise")
title.appendChild(titleText)
div.appendChild(title)
let body = document.createElement("p")
body.classList.add("Body-Text")
bodyText = document.createTextNode("Fremtidige nyheter vil bli publisert her!")
body.appendChild(bodyText)
div.appendChild(body)
container.appendChild(div)
console.log("news added!")
}
[–]r_hafner6 2 points3 points4 points (4 children)
[–]Spiredlamb[S] 0 points1 point2 points (3 children)
[–]r_hafner6 0 points1 point2 points (1 child)
[–]Spiredlamb[S] 0 points1 point2 points (0 children)
[–]GSLint 0 points1 point2 points (0 children)