Node.JS 字符串编码问题

发布于 2024-12-20 08:45:45 字数 1323 浏览 0 评论 0原文

我正在编写 TCP 聊天服务器和 SQL Server 之间的一种接口,并且在处理用户提交值并分配从该数据库中具有该值的行中提取的命名的部分时。当使用 telnet shell 中的值查询此数据库时,我无法从数据库中获得任何结果。但是当我在 adminer/MySQL @ BASH 等中执行相同的查询时我可以...... 我的想法是,这归结为编码问题。对于节点来说相当菜鸟,我真的不知道该怎么办。我确实对 JavaScript 有很好的经验,但不是 Node。

代码

function setCliNameOrKick(client, key){
key = String(key).replace(/\n\r\b\\\s/gi, "");
var q = "SELECT username FROM webusers WHERE lic = \'"+String(key).toString()+"\'; --";
console.log(key);
query(q);
cli.query(q, function cb(e, r, f){
    if(client != null){
        console.log(r);
        if(r.length >= 1){
            client.name = r['username'];
        }else{
            client.stream.end();
        }
    }else{
        console.log("Was Passed A Null Client!");
    }

});
}

来自数据库查询工具的 它从客户端在连接时发送的字符串以及表示客户端的对象获取输入

 stream.addListener("data", function(data){
    if(client.name == null){
        data = String(data).replace(new RegExp("[\n]+", "g"), "");
        cNameBuff = cNameBuff + data;
        if(cNameBuff.length > 1){ //Min Length
            //client.name = cNameB;
            db.set(client, cNameBuff);
            onAuth(client);
        }
        return;
    }
    data = String(data);
    if(data.length >= 2){
        srv.procChat(client, data);   
    }
});

I'm writing a sort of interface between a TCP Chat server and an SQL Server, and while working on a part where a user submits a value and is assigned a named pulled from a row in this DB with this value. When querying this DB with a value from a telnet shell, I can't get any results from the DB. But I can when I perform the same query in adminer/MySQL @ BASH etc...
My thoughts are that it has come down to an encoding issue. Being rather noobish to node, I don't really know what to do. I do have a pretty good experience with JavaScript, just not node.

Code

function setCliNameOrKick(client, key){
key = String(key).replace(/\n\r\b\\\s/gi, "");
var q = "SELECT username FROM webusers WHERE lic = \'"+String(key).toString()+"\'; --";
console.log(key);
query(q);
cli.query(q, function cb(e, r, f){
    if(client != null){
        console.log(r);
        if(r.length >= 1){
            client.name = r['username'];
        }else{
            client.stream.end();
        }
    }else{
        console.log("Was Passed A Null Client!");
    }

});
}

That comes from the DB query tool
It takes input from a string sent by the client on connect, alongside an object representing the client

 stream.addListener("data", function(data){
    if(client.name == null){
        data = String(data).replace(new RegExp("[\n]+", "g"), "");
        cNameBuff = cNameBuff + data;
        if(cNameBuff.length > 1){ //Min Length
            //client.name = cNameB;
            db.set(client, cNameBuff);
            onAuth(client);
        }
        return;
    }
    data = String(data);
    if(data.length >= 2){
        srv.procChat(client, data);   
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文