main.js
jsonReader('./source/locations.json', (err, data) => {
if (err) {
console.log(err)
return
}
for (let i = 0; i <= 10; i++) {
//console.log(data.towns[i].townName);
locationsout += `Town ${i+1} : ` + stringify(data.towns[i].townName) + "\n";
}
interaction.reply({
content: `\`\`\`TOWNS: \n${locationsout}\`\`\``,
})
})
So I'm taking data from a json file I have that looks like this
"towns":[
{
"townId": 1,
"townName": "Pallet Town",
"canTravel": 1,
"hasMart": 0,
"hasCenter": 0,
"hasGym": 0,
"canFish": 1,
"locationImage":"https://cdn2.bulbagarden.net/upload/7/77/Pallet_Town_FRLG.png"
},
but with 10 more similar towns and I'm trying to be able to output a reply which requires a string but I'm having trouble accessing it and transforming it to a string that I can use in javascript. Currently with this code the error I'm facing is
TypeError: Cannot read properties of undefined (reading 'townName')
but I've also had it somewhat working when using only stringify(data.towns[i]) but that outputs the entire object which i dont want I just want the townName part So I also tried stringifying then parsing it into a javascript object like so const obj = JSON.parse(tname)using let tname = stringify(data.towns[i]) but that resulted in a
SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse ()
And I just don't know what to make of that.