main.js中有一段代码:
var test_mysql = require('./test_mysql.js')
... //some code
before(function(){
test_mysql.preparingDB(test_mysql.SQL_query.clear_data); // or test_mysql.preparingDB(SQL_query.clear_data);
});
test_mysql.js 中的代码:
var SQL_query = require('./SQL_query.js');
...//some code
exports.preparingDB = function(query){
connection.connect(function(err){
if (err){
console.error('Error connecting: ' + err.stack);
return;
}
console.log('Connected as id ' + connection.threadId);
});
console.log(query);
connection.query(query, function(err){
if (err){
console.error('Error: ' + err.stack);
throw err; /*не уверена, что оба сработают*/
}
console.log('Database preparation completed');
});
SQL_query.js 中的代码:
exports.clear_data = 'SET FOREIGN_KEY_CHECKS=0; -- \
TRUNCATE TABLE `billing_payment_gateway`; -- \
TRUNCATE TABLE `fos_user`; -- \
TRUNCATE TABLE `billing_account_has_product`; -- \
TRUNCATE TABLE `billing_account`; -- \
TRUNCATE TABLE `billing_product`; -- \
SET FOREIGN_KEY_CHECKS=1;'
<小时/>
当我尝试运行 main.js 时,我收到错误。第一种情况是TypeError:无法读取未定义的属性“clear_data”
。
另一种情况是 ReferenceError: SQL_query is not Defined
。
附注好的。请向我解释一下。某人。在 test_mysql.js
中还需要 var mysql = require('mysql');
为什么这个模块在 main.js
中可见???
最佳答案
在 test_mysql.js
中,您仅导出 preparingDB
,因此这是该模块中唯一可从外部使用的内容。
如果您希望从需求模块中使用它,您还应该导出 SQL_query
变量
关于javascript - 为什么从导入的文件中看不到该变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35023612/