Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

javascript - Pulling emoji URL from .json gives undefined

so I am working on a bot, and I have the following code. I am attempting to get a color name from the args[1] of the message and use the color name to figure out which emoji link to grab, and have the following code to pull that from the json in the bot -

const store = require(‘./../../items.json’);

// inside module.exports -
const item = args[0];
const col = args[1];

let name = store[item].name;
let price = store[item].price;
let desc = store[item].description;

let emote = store[item].col // trying to grab the emote link from the variable

When put into an embed, it returns “undefined” for the emote variable. The below code is the json bit (in this case, the item variable is fish) -

{
   “fish”: {
      “name”: “fish”,
      “price”: “$1”,
      “description”: “<description here>“,

      “white”: “https://<white fish emoji link here>”,
      “pink”: “https://<pink fish emoji link here”
    }
}

I have asked in multiple discord servers, and have looked around online for answers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use an if else statement the way you are doing you cant fetch the object values with an variable with your way you can check if col === pink then give emote value x if col === blue give emote value y and so on like so


if(col === 'pink'){
let emote = store[item].pink
}

Edit Use let instead of const


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...