【问题标题】:Cross referencing JSON and MySQL?交叉引用 JSON 和 MySQL?
【发布时间】:2019-08-27 08:48:53
【问题描述】:

我为一个不和谐的机器人编写了一个“购买”功能,它将项目的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.`)
        };

长话短说,我需要从id: 中提取name: 并与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 之前进行测试)

【问题讨论】:

    标签: javascript mysql node.js json discord.js


    【解决方案1】:

    设法解决了这个问题:

    使用Object.values(item) 将 JSON 对象值转换为数组,然后在将 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"}`);
    

    【讨论】:

    • 我将每个项目的 ID 更改为具有不同的 ID 以及 mySQL 中的字段类型为int。所以在这种情况下,它将是:function getItemName(id) {return Object.values(items)[id -1].name} let horseName = (isNaN(horse) || horse == null)? "None" : getItemName(horse); let weaponName = (isNaN(weapon) || weapon == null)? "None" : getItemName(weapon); let armourName = (isNaN(armour) || armour == null)? "None" : getItemName(armour);
    猜你喜欢
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多