I want to take the names from data.json and post it on a discord embed. For example I tried to print Animal Skull in a embed but it gave me that error.
error
JavaScript
x
2
1
Unhandled Rejection: TypeError: Cannot read properties of undefined (reading '0')
2
test.js
JavaScript
1
21
21
1
const { EmbedBuilder } = require("discord.js");
2
const datapath = ('data.json')
3
4
module.exports = {
5
config: {
6
name: "test",
7
description: "Just a test command!",
8
},
9
permissions: ['SendMessages'],
10
owner: false,
11
run: async (client, message, args, prefix, config, db) => {
12
13
message.reply({ embeds: [
14
new EmbedBuilder()
15
.setDescription(datapath.names[0])
16
.setColor("Green")
17
] })
18
19
},
20
};
21
my data.json
JavaScript
1
13
13
1
[
2
{
3
"names": "Animal Skull",
4
"itemtype": "Block, 32, 4, No, Yes",
5
"image": "https://static.wikia.nocookie.net/cubixworlds/images/4/4c/Animal_Skull.png/revision/latest?cb=20220720212850"
6
},
7
{
8
"names": "Asteroid Block",
9
"itemtype": "Block, 1, 1, No, Yes",
10
"image": "https://static.wikia.nocookie.net/cubixworlds/images/f/fe/Asteroid_Block.png/revision/latest?cb=20220725063800"
11
}
12
]
13
I would be really gratefull if anyone can help me.
Advertisement
Answer
You should use require
to get data from json
JavaScript
1
2
1
const datapath = require('./data.json')
2
And your json contains an array of objects so use datapath[0].names
to get Animal skull