Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 带有process.nextTick的节点mysql查询_Node.js_Node Mysql - Fatal编程技术网

Node.js 带有process.nextTick的节点mysql查询

Node.js 带有process.nextTick的节点mysql查询,node.js,node-mysql,Node.js,Node Mysql,以下是我的简化问题: 我想将我的数据库功能分离到另一个文件中。它不工作,我必须使用process.nextTick 下面是db文件: // db_functions function selectUserByEmail (email){ client.query("select * from users where email = ?", email, function(err,rows){ if(err) { throw err; } return ro

以下是我的简化问题:

我想将我的数据库功能分离到另一个文件中。它不工作,我必须使用process.nextTick

下面是db文件:

// db_functions
function selectUserByEmail (email){
    client.query("select * from users where email = ?", email, function(err,rows){
        if(err) { throw err; }
        return rows;
    });
}
module.exports.selectUserByEmail = selectUserByEmail;

我怎样才能解决这个问题?谢谢你的帮助。

这不应该也可能根本不起作用。db.selectUserByEmail'levon'很可能是异步的,并且很可能返回未定义的,所以您得到未定义错误的无法读取属性'length'就不足为奇了


问题是,你为什么不在第一种情况下得到它?这真的很简单:你又一次,很可能进入了一个try..catch区块。有些框架将您的代码封装在这些框架中,但process.nextTick调用稍后会提供函数,因此请尝试..catch无效。

您是对的。。。第一种情况也是间歇性的;我将修改我的问题;必须是因为异步的性质。我所做的只是将所有mysql查询/函数分离到一个不同的文件中。有什么建议吗?这是异步函数问题的基本返回。选中此项:
// db_functions
function selectUserByEmail (email){
    client.query("select * from users where email = ?", email, function(err,rows){
        if(err) { throw err; }
        return rows;
    });
}
module.exports.selectUserByEmail = selectUserByEmail;