javascript - 交叉引用 JSON 和 MySQL?
问题描述
我已经为一个不和谐的机器人编写了一个“购买”功能,它将id
项目的 插入到 mysql 中的相应表中。
购买示例:
const join = args.join(" ")
let item = JSON.parse(fs.readFileSync('commands/economy/resources/items.json', "utf8"));
//Horses
const Horse1 = item.Horse1
//Buy Horse1
if (join === Horse1.name) {
let itemName = Horse1.name
let itemLuck = Horse1.luck
let itemId = Horse1.ID.toString()
let itemPrice = Horse1.price.toString()
//Account Check
if (rows.length < 1) { return message.channel.send("You do not have an account with The Iron Bank. Use ``;start`` to open one.") };
//Item Check
if (rows[0].horse === itemId) { return message.channel.send(`❌ | You already have a ${itemName}!`) }
//Money Check
if (rows[0].bank < itemPrice) { return message.channel.send(`You do not have enough coins in your account to buy **${itemName}**. It costs ${itemPrice} coins. You have ${rows[0].bank} coins`) };
//DB Update
con.query(`UPDATE economy SET horse = '${itemId}', horseluck = '${itemLuck}', bank = bank-${itemPrice} WHERE user = ${message.author.id}`)
//Bought
message.channel.send(`✅ | Purchased ${itemName} for ${itemPrice} coins.`)
};
长话短说,我需要从中提取name:
并id:
与rows[0].horse
/交叉引用它rows[0].weapon
这是我的 mySQL 选择查询中的代码:
//Direct to start if not in DB
if (rows.length < 1) {
return message.channel.send("You do not have an account with The Iron Bank. Use ``;start`` to open one.")
};
let horse = rows[0].horse;
let weapon = rows[0].weapon;
let armour = rows[0].armour;
let luck = (rows[0].horseluck + rows[0].weaponluck + rows[0].armourluck)
console.log()
return message.channel.send(`**Horse**: ${horse}\n**Weapon**: ${weapon ? weapon : "None"}\n**Armour**: ${armour ? armour : "None"}\n**Luck Bonus**: ${luck ? "+"+luck+"%" : "None"}`);
但是上面打印出马的 ID 而不是message.channel.send
. 我如何将 MySQL 中的 ID 交叉引用到 JSON 中的 ID 并在其中打印出名称message.channnel.send
?
Example of my JSON file:
{
"Horse1":{
"ID": 1,
"name": "Dornish Sand Steed",
"description": "Splendid beasts",
"type": "Horses",
"price": "100000",
"luck": "5.0"
},
"Horse2":{
"ID": 2,
"name": "Arabian",
"description": "Preferred war mounts",
"type": "Horses",
"price": "50000",
"luck": "2.0"
},
"Weapon1":{
"ID": 1,
"name": "Longclaw",
"description": "Valyrian steel sword that was the ancestral weapon of House Mormont",
"type": "Weapons",
"price": "100000",
"luck": "5.0"
},
"Weapon2":{
"ID": 2,
"name": "Oathkeeper",
"description": "Valyrian steel sword made from House Starks greatsword",
"type": "Weapons",
"price": "50000",
"luck": "2.0"
},
}
(我知道这段代码中的 SQL 查询容易出现 SQL 注入。这只是为了在我开始使用 mysql.format 之前进行测试)
解决方案
设法解决了这个问题:
将 JSON 对象值转换为数组,Object.values(item)
然后在将 rows[0] 值转换为 INT 后使用 ID 索引数组,如下所示。如果该值未定义(在这种情况下为 null,因为它不能具有除 JSON 中指定的 ID 之外的任何其他值),则视为“无”。
let horse = rows[0].horse;
let weapon = rows[0].weapon;
let armour = rows[0].armour;
let luck = (rows[0].horseluck + rows[0].weaponluck + rows[0].armourluck)
function getItemName(id){
return (id == undefined)? "None" : Object.values(item)[id - 1].name;
}
let horseName = getItemName(parseInt(horse))
let weaponName = getItemName(parseInt(weapon) + 5)
let armourName = getItemName(parseInt(armour) + 10)
//5 items of each type
return message.channel.send(`**Horse**: ${horseName}\n**Weapon**: ${weaponName}\n**Armour**: ${armourName}\n**Luck Bonus**: ${luck ? "+"+luck+"%" : "None"}`);
推荐阅读
- javascript - 如何获取具有相同密钥javascript的json数据
- c++ - 为什么我在执行 C++ 代码时出错
- python - 评估一个 numpy 数组
- javascript - Angular 9 - 如何在 HTML 页面中动态添加 mat-icon?
- setcookie - 浏览器不保存 cookie
- c# - C# WSDL API 如何访问数组?
- android - 为什么是这个列表
没有在 Room 数据库中存储任何数据? - elasticsearch - 带有 XML 文件的 Logstash
- spring-boot - 如何在 Spring Boot / JSP 中显示一组数组?
- dji-sdk - M300 OSDK - 未获得无人机版本