Im fetching data on my server from a exteranal API, then im trying to send that data to an ejs file using JSON.stringify(data), and trying to read the data in the ejs file with JSON.parse(data), but im either getting something like [Object object] or a string with a lot of weird characters in it, i tried using replace, but it still doesnt work.
This is what i have on the server side.
router.get("/api", (req, res) => {
const info = {
place: "London",
country: "UK",
temp: "16",
weather: "Cloudy",
};
res.render("weathery", { user: JSON.stringify(info) });
}
This is what i have on the ejs file side.
<script>
var data = "<%= user %>";
const newData = data;
console.log(newData);
</script>
This is what it prints without using JSON.parse
{"place":"London","country":"UK","temp":"16","weather":"Cloudy"}
i tried using replace on the weird characters like #,&,; and 34 and it didnt work, also that would lead to a problem on some cases if was receving some data with the number 34
Whenever i tried to parsed it send an error "Uncaught SyntaxError: Unexpected token & in JSON at position 1"
Hopefully you can help me, have a nice day :)