Javascript 函数不可见外部变量(未定义错误)-什么';问题是什么?

Javascript 函数不可见外部变量(未定义错误)-什么';问题是什么?,javascript,node.js,mocha.js,node-mysql,Javascript,Node.js,Mocha.js,Node Mysql,我不明白错误是什么。这是我测试mysql3.js的代码: var mysql = require('mysql'); var SQL_query = require('./SQL_query.js'); exports.сonnection = mysql.createConnection({ host: '...', user: '...', password: '...', database: '...', connectTimeout: 6048

我不明白错误是什么。这是我测试mysql3.js的代码:

var mysql = require('mysql');
var SQL_query = require('./SQL_query.js');

exports.сonnection = mysql.createConnection({
    host: '...',
    user: '...',
    password: '...',
    database: '...', 
    connectTimeout: 604800,
    multipleStatements: true
});

exports.preparingDB = function(query){
    /*console.log(query); */ 
    сonnection.query(query, function(err){
        if (err){       
            console.error('Error: ' + err.stack);
            throw err; 
        }
        console.log('Database preparation completed');
    });
};
var сonnection = require('./test_mysql3').сonnection;
/*console.log(сonnection); 

console.log(typeof(сonnection)); */
var preparingDB = require('./test_mysql3').preparingDB;

var SQL_query = require('./SQL_query.js');

before(function(done){
    /*console.log('before');*/ 
    сonnection.connect(function(err){
        if (err){
            console.error('Error connecting: ' + err.stack);
            return;
        }
        console.log('Connected as id ' + сonnection.threadId);
        flow.execute(function(){
            return done();
        });
    });
});

describe('Check the Email field of the registration form.', function(){
    before(function(){
        preparingDB(SQL_query.needForReg);
    });
    it('Enter an already registered Email', function(done){     
        var inputEmail = '123@ya.ru';
        var inputPassword   = '12345678Aa';
        var errElement = "//*[@class='form-errors server-error']";
        var errMessage = 'Email already in use';
        loadRegistrationForm();
        fillingRegistrationForm(inputEmail, inputPassword, errElement, errMessage);
        flow.execute(function(){
            return done();  
        });
    });
    after(function(){
        preparingDB(SQL_query.clear_db);
    });
这是我的
main.js
代码:

var mysql = require('mysql');
var SQL_query = require('./SQL_query.js');

exports.сonnection = mysql.createConnection({
    host: '...',
    user: '...',
    password: '...',
    database: '...', 
    connectTimeout: 604800,
    multipleStatements: true
});

exports.preparingDB = function(query){
    /*console.log(query); */ 
    сonnection.query(query, function(err){
        if (err){       
            console.error('Error: ' + err.stack);
            throw err; 
        }
        console.log('Database preparation completed');
    });
};
var сonnection = require('./test_mysql3').сonnection;
/*console.log(сonnection); 

console.log(typeof(сonnection)); */
var preparingDB = require('./test_mysql3').preparingDB;

var SQL_query = require('./SQL_query.js');

before(function(done){
    /*console.log('before');*/ 
    сonnection.connect(function(err){
        if (err){
            console.error('Error connecting: ' + err.stack);
            return;
        }
        console.log('Connected as id ' + сonnection.threadId);
        flow.execute(function(){
            return done();
        });
    });
});

describe('Check the Email field of the registration form.', function(){
    before(function(){
        preparingDB(SQL_query.needForReg);
    });
    it('Enter an already registered Email', function(done){     
        var inputEmail = '123@ya.ru';
        var inputPassword   = '12345678Aa';
        var errElement = "//*[@class='form-errors server-error']";
        var errMessage = 'Email already in use';
        loadRegistrationForm();
        fillingRegistrationForm(inputEmail, inputPassword, errElement, errMessage);
        flow.execute(function(){
            return done();  
        });
    });
    after(function(){
        preparingDB(SQL_query.clear_db);
    });
关于调试打印:

/*console.log(сonnection); 

console.log(typeof(сonnection)); */
这是调试打印。现在它被注释掉了。我想检查是否正在获取
连接
对象。如果取消注释,控制台中的调试打印将显示
连接
对象的结构和单词“object”。我的意思是,我得到了
连接
对象

/*console.log('before');*/ 
这也是一个调试打印。在这里,我想检查在加载
test_mysql3.js
之前启动
main.js
中的代码时,将较早地处理哪些代码以避免错误。 如果您取消注释它,您可以看到,首先加载
test\u mysql3.js
,然后运行下面的代码

我认为这段代码应该没有问题,但当我在控制台中运行main.js时,它会收到一个错误:

ReferenceError: сonnection is not defined
      at exports.preparingDB (C:\Program Files\nodejs\test\test_mysql3.js:15:2)
      at Context.<anonymous> (C:\Program Files\nodejs\test\test4_mocha.js:34:3)
      at callFn (C:\Users\Valentine11\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:286:21)

Done
回调需要等待函数的执行。但问题仍然存在。

preparingDB是异步的。您没有等待它完成。只是猜测一下:您可能需要在
preparingDB
中使用
exports.connection.query
function@jfriend00我需要做些什么来解决这个问题?@Hans Kesting,“connection.query”会随着对数据库的不同查询而被反复调用,所以我将它包装在一个函数中,为了能够传递一个参数'query',请尝试在preparingDB函数中使用该连接之前添加一个
console.log(Сconnection)
(可能还有“before”)-尝试并找出未定义的连接