当我运行此代码时由于某种原因尝试在创建之前更新一行,并且无法读取rows.xp的值
client.on("message", (message) => {
if (message.author.bot) return;
const parse = require("pg-connection-string");
const { Pool } = require ('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL.parse,
port: 5432,
host: process.env.dbhost,
database: process.env.db,
user: process.env.user,
password: process.env.password,
ssl: true,
});
pool.connect(err => {
if(err) throw err;
console.log('Connected to PostgresSQL');
})
pool.query(`SELECT xp, level FROM xp WHERE userid = '${message.author.id}'`, {useArray: true}, (err, rows) => {
const curlvl = Math.floor(0.1 * Math.sqrt(rows.xp + 0.1));
const xpgen = Math.floor(Math.random() * (20 - 5 + 1)) + 5;
if(err) throw err;
let sql;
if (rows.length < 1){
sql = `INSERT INTO xp(userid, xp, level) VALUES('${message.author.id}', 0, 0)`
} else {
let xp = rows.xp;
sql = `UPDATE xp SET xp = ${xp + xpgen} WHERE userid = '${message.author.id}'`
}
pool.query(sql, console.log);
pool.end(err => {
if(err) throw err;
console.log('Not logged to PostgresSQL');
});
});
});
Heroku上的控制台每次只说UPDATE ...但是当我自己输入我的用户ID时,代码有时会起作用...
有人可以帮我解决问题吗?答案 0 :(得分:0)
问题已解决,PostgreSQL正确地从0开始计数...
pool.query(`SELECT * FROM xp WHERE userid = '${message.author.id}'`,(err, result) => {
if (!result.rows[0]){{
....