Welcome to OGeek Q&A 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...