未定义Discord.js和PostgreSQL行hbucks

时间:2018-09-16 22:35:44

标签: javascript node.js postgresql discord.js

重复帖子:这是由于我不小心删除了我的帐户...不要问为什么。我只是笑了。我为这是重复的帖子表示歉意,但现在我无法再访问该帖子。

最近,对于新的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完全没有问题

1 个答案:

答案 0 :(得分:0)

改为使用此:

// 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;
// ....