DUPLICATE POST:这是因为我不小心删除了我的帐户...不要问为什么。我只是做了哈哈。我很抱歉这是一个重复的帖子,但我再也无权访问该帖子了。
我最近刚刚从我的新Discord.js机器人的数据库切换到MySQL到PostgreSQL。我一直得到一个错误,说行hbucks未定义或未定义。代码如下。
let mysql = require("pg");
function generateXp(){
let min = 1;
let max = 3;
return Math.floor(Math.random() * (max - min)) + min;
}
module.exports = (bot, message, args, con) => {
con.query(`SELECT * FROM hbucks WHERE id = ${message.author.id} AND guildid = ${message.guild.id}`, (err, rows) => { //Where id defines what row to use, this is stating that you are going to organize it by the id
if(err) throw err;
let sql;
let username = message.author.username
let authorid = message.author.id
if(rows.length < 1) {
sql = `INSERT INTO hbucks (id, username, guildid, hbucks) VALUES (${message.author.id}, '${username}', ${message.guild.id}, ${generateXp()})`
con.query(sql);
} else {
let hBucks = rows.fields[0].hbucks;
sql = `UPDATE hbucks SET hbucks = ${hBucks + generateXp()} WHERE id = ${authorid} AND guildid = ${message.guild.id}`;
con.query(sql, console.log);
sql = `UPDATE hbucks SET username = '${username}' WHERE id = ${authorid} AND guildid = ${message.guild.id}`;
con.query(sql, console.log);
}
}
);
}
错误是让hBucks = rows [0] .hbucks是未定义的。这段代码似乎有用,并且对MySQL没有任何问题
请改用:
// Assuming you want the whole object.
let hBucks = rows.fields[0];
这是rows.fields [0]对象:
{name: "id", tableID: 16395, columnID: 5, dataTypeID: 20, dataTypeSize: 8, …}
您可以访问其字段,如下所示:
var id= rows.fields[0].name;
var tableId = rows.fields[0].tableID;
var columnID = rows.fields[0].columnID;
// ....