【问题标题】:Node.js Discord.Js MysqlNode.js Discord.Js Mysql
【发布时间】:2021-11-26 20:01:14
【问题描述】:

我正在创建一个 Discord 机器人。当我们询问错误时,机器人会给出有关该错误的答案。但是当我得到 MySQL 数据时,我正在尝试将其发布到 Discord 但它说undefined

你能帮帮我吗?

const Discord = require("discord.js");
const client = new Discord.Client();
const ayarlar = require("./ayarlar.json");
const mysql = require('mysql');

var baglanti = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'hatabot'
});

baglanti.connect(function (err) {
  if (err) throw err;
    console.log("baglanti basarili")
client.on('message', msg => {

    baglanti.query("SELECT * from hatalar WHERE hata_komut LIKE "+'%'+msg.content+'%',function(err,sonuc){
 
      console.log(msg.content+" kelimesine ait\n\n"+sonuc+"\n\nsonucu bulundu");
    });
});
});


client.login(ayarlar.token)

【问题讨论】:

    标签: php mysql node.js discord.js


    【解决方案1】:

    查询返回数组,这就是为什么普通的result 或在你的情况下sonuc 将返回数组,因此无法正常工作。它也可能有 0 个结果,所以你需要先检查一下

    baglanti.query("SELECT * FROM hatalar WHERE hata_komut LIKE "+'%'+msg.content+'%',function(err,sonuc){
         if(sonuc.length > 0){
         //Check if there is any rows
         console.log(sonuc[0].column);
         }
    });
    

    所以要深入研究;

    由于查询返回数组,需要选择其中的哪一项,sonuc[0] 是数组中的第一项。我写的第二件事是表的实际列,所以假设你有带有id, user, email 列的数据库,并且你想显示用户列,你会做sonuc[0].user

    【讨论】:

    • 谢谢你的回答,但我没有把我的东西放在一起。如果我尝试像你一样伤心我收到类型错误,因为它无法读取“未定义”的长度。即使我写了关键字它也没有回答我的结果。
    猜你喜欢
    • 2021-04-14
    • 2021-08-14
    • 2021-12-17
    • 2021-08-05
    • 2021-09-11
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多