Answer the question
In order to leave comments, you need to log in
How to convert JSON sent from server to JSON in HTML page?
There is a collection with data in MongoDB. Literally, this is a list of students. You need to output them to HTML so that you can output otherwise than just
{{#each Students}}
{{this}}
{{/each}}
Failed to convert the received HTML String to JSON back, which was sent by the server. I'm powdering something.
The idea in the output is standard:
{{#each Students}}
Name: {{this.name}}, Surname: {{this.surname}} etc.
{{/each}}
Answer the question
In order to leave comments, you need to log in
Issue resolved.
adminRouter.get('/lists-of-the-bests', async (req, res) => {
const bestStudents = await BestStudent.find({})
const BS = JSON.stringify(bestStudents)
res.render('lists -of-the-bests', {
title: 'Best Lists',
BS
})
})
const string = document.querySelector('.sss').innerHTML
console.log(string)
const json = JSON.parse(string)
console.log(json)
for (let key in json) {
console.log(json[key])
document.querySelector('.ssss').innerHTML += `${json[key].name} ${json[ key].secondName} ${json[key].thirdName}Achievement: ${json[key].achive}
Class: ${json[key].grade}`
}
where .'sss' is a container with display:none, with the template {{BS}}
I could not give birth to anything better in a day.
UPD: instead of this perversion, you can use the lean method after find : .find({}).lean()
Then student.name,..., etc. will work.
Lean returns a JS object
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question