简体   繁体   English

将整数传递给查询时,NodeJS MySQL返回空数组

[英]NodeJS MySQL Returning Empty Array When Passing Integers To Query

SECOND EDIT 第二编辑

The issue was caused by the MAX_INTEGER_VALUE which is lower then the integer value I was passing. 问题是由MAX_INTEGER_VALUE引起的,该值比我传递的整数值低。 I changed the MySQL table column to TEXT instead of BIGINT and everything is being returned correctly. 我将MySQL表列更改为TEXT而不是BIGINT ,所有内容BIGINT正确返回。

Thanks for all the help! 感谢您的所有帮助!

EDIT 编辑

So I just realized that the userID variable and the guildID variables are being passed using this line of code. 因此,我刚刚意识到使用此行代码传递了userID变量和guildID变量。

mysqlModule.userCrewSearch(575783451018526744, 282997056438665217);

However the values that are being supplied to the SQL statement turn the last two digits of the number into '00'. 但是,提供给SQL语句的值会将数字的最后两位数字变为'00'。 So instead of 575783451018526744 the value being passed into the SQL statement is 575783451018526700 . 因此,传递给SQL语句的值不是575783451018526744 ,而是575783451018526700

编辑的问题图片

So why is this value being changed when nothing I am doing in my code is changing these values? 那么,当我在代码中没有做任何事情来更改这些值时,为什么还要更改此值?

Original Post 原始帖子

I'll keep this short and sweet. 我会简短而甜美的。 I'm trying to run a query using the nodejs MySQL package. 我正在尝试使用nodejs MySQL包运行查询。 I'm not sure where I'm going wrong but whenever I call my function that executes my query, I'm always returned an empty array, unless I hardcode the values into the SQL query. 我不确定哪里出错了,但是每当我调用执行查询的函数时,总是返回一个空数组,除非我将值硬编码到SQL查询中。

Heres the code: 这是代码:

// Search for the User's Crew
function userCrewSearch(guildID, userID) {
    pool.getConnection(function(err, connection) {
        if(err) { 
            return console.log(err);
        }
        var sql = "SELECT * FROM `crew-members` WHERE `userID`=? AND `guildID`=?;";
        console.log(sql);
        connection.query(sql, [guildID, userID], function(err, results) {
            connection.release(); // always put connection back in pool after last query
            if(err) { 
                return console.log(err);
            }
            return console.log(results);
        });
    });
}

I'm calling this function like so: userCrewSearch(575783451018526744, 282997056438665217); 我这样调用此函数: userCrewSearch(575783451018526744, 282997056438665217);

Both of the values I'm passing are integers. 我传递的两个值都是整数。 However this is what I get in my console. 但是,这就是我在控制台中得到的。

密码破损

However, here is my code with the values hardcoded into the SQL... to which the code then returns the result in the form of a RowDataPacket. 但是,这是我的代码,其中值硬编码到SQL中,然后代码以RowDataPacket的形式返回结果。

// Search for the User's Crew
function userCrewSearch(guildID, userID) {
    pool.getConnection(function(err, connection) {
        if(err) { 
            return console.log(err);
        }
        var sql = "SELECT * FROM `crew-members` WHERE `userID`=282997056438665217 AND `guildID`=575783451018526744;";
        console.log(sql);
        connection.query(sql, [guildID, userID], function(err, results) {
            connection.release(); // always put connection back in pool after last query
            if(err) { 
                return console.log(err);
            }
            return console.log(results);
        });
    });
}

Heres the result. 继承人的结果。

工作守则

Bonus Question: How do I handle the RowDataPacket? 奖励问题:如何处理RowDataPacket? Can I convert this directly into an object so I can call results.crewID and return just that value? 我可以将其直接转换为对象,以便可以调用results.crewID并仅返回该值吗?

Same problem i was facing few days ago. 我几天前面临的同样问题。 I have solved this by converting the parameters into string. 我已经通过将参数转换为字符串解决了这一问题。

function userCrewSearch(String(guildID), String(userID)) {

       //  your code here 
}

Try adding + before your numeric parameter, it converts into number, it worked for me- 尝试在数字参数前添加+,它会转换为数字,对我有用-

connection.query(sql, [+guildID, +userID], function(err, results) {

for your bonus questions answer, you can directly access the crewID or some other key using, 对于您的奖励问题答案,您可以使用,直接访问crewID或其他一些键,

results[0].crewID

or do something like - 或做类似的事情-

const [ result ] = results;
console.log(result.crewID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM