mysql - nodejs 上的同步功能到 MySQL

标签 mysql node.js

我有一个带有 MySQL 查询的检查脚本,问题是授权函数是在完成 MySQL 查询之前执行的,所以它总是返回 false。怎么解决?

 mysql= require('mysql');
 var connection = mysql.createConnection({});


 check_auth(req.query.s){
 var sql = "query to mysql";
    connection.query(sql, function(err, results) {
        if (err) throw err;
        if (results.length  > 0) {
            return true;
        }else{
            return false;
        } 
}

io.configure(function () {
io.set('authorization', function(req, callback) {
var auth=check_auth(req.query.s);
console.log(auth);
if (auth===true){
    return callback(null, true);
}else{
return callback('notauth',false);
}
});
});

编辑:像 Jarema 这样的新结构,但仍然不起作用

 mysql= require('mysql');
 var connection = mysql.createConnection({});


function check_auth(input, callback){
 var sql = "query to mysql";
    connection.query(sql, function(err, results) {
        if (err) callback(err);
        if (results.length  > 0) {
            callback(null,results.values);
        }else{
            callback(null, false);
        } 
}

io.configure(function () {
    io.set('authorization', function(req, callback) {
        check_auth(req.query.s, function(err, result) {
            if (err) {
                //handle that error somehow. we dont not structure of Your func.
                return console.log('error:(');

            }
            if(result === false) {
                return callback('notauth', false);
            } else {
                return callback(null, result);;
            }
        });
    });
});

错误是

 TypeError: undefined is not a function
at Query._callback (myserver.js:54:12)
at Query.Sequence.end (node_modules/mysql/lib/protocol/sequences/Sequence.js:78:24)
at Query._handleFinalResultPacket (node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
at Query.EofPacket (node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
at Protocol._parsePacket (node_modules/mysql/lib/protocol/Protocol.js:205:24)
at Parser.write (node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (node_modules/mysql/lib/Connection.js:73:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)

第54行是

 callback(null,results.values);

最佳答案

如果调用下一个函数/if 语句时 check_auth 查询仍在处理中,对我来说这意味着它是异步的。如果它是同步的,它将阻止代码执行直到完成。

编辑:正如 Jonatham 提到的,查询函数本身可能是异步的。如果你把它放在“普通”同步函数中,它确实意味着你可以将内部异步结果返回给父函数,然后从父函数再次返回它。您必须处理回调。

这是一个可能有效的例子,肯定需要调整,因为我们不知道 auth 函数里面有什么:

 mysql= require('mysql');
 var connection = mysql.createConnection({});


    function check_auth(input, callback){
     var sql = "query to mysql";
        connection.query(sql, function(err, results) {
            if (err) callback(err);
            if (results.length  > 0) {
                callback(null, true);
            }else{
                callback(null, false);
            } 
    }

    io.configure(function () {
        io.set('authorization', function(req, callback) {
            check_auth(req.query.s, function(err, result) {
                if (err) {
                    //handle that error somehow. we dont not structure of Your func.
                    return console.log('error:(');

                }
                if(result === true) {
                    return callback(null, true);
                } else {
                    return callback('notauth', false);
                }
            });
        });
    });

关于mysql - nodejs 上的同步功能到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23768043/

相关文章:

mysql - 将日期字段转换为仅返回日期字段

javascript - Angular 5 http post方法在nodejs中未调用

django - 在 Django 中将 JSON 数据从响应传递到请求

javascript - MongoDB RESTful API 结构

MySQL NOW() 查询

javascript - 使用 Ajax 将响应从 PHP 返回到 Jquery

mysql - 如何根据其他列找到一列的不同之处

C# DGV - 显示实时 MYSQL 数据库表

node.js - Mongoose 引用被覆盖

node.js - 由于 JSON 字符串中存在尾随空格,无法解析 JSON