为什么变量在导入的文件中不可见?
Why the variable is not visible from the imported file?
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: Cannot read property 'clear_data' of undefined
。
另一种情况是 ReferenceError: SQL_query is not defined
.
为什么?什么问题?
P.S。行。请给我解释一下。有人。 In test_mysql.js
also require var mysql = require('mysql');
Why this module is visible in main.js
?????
在您的 test_mysql.js
中,您仅导出 preparingDB
,因此这是此模块中唯一可从外部使用的内容。
如果您希望从需要的模块中获得 SQL_query
变量,您还应该导出它
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: Cannot read property 'clear_data' of undefined
。
另一种情况是 ReferenceError: SQL_query is not defined
.
为什么?什么问题?
P.S。行。请给我解释一下。有人。 In test_mysql.js
also require var mysql = require('mysql');
Why this module is visible in main.js
?????
在您的 test_mysql.js
中,您仅导出 preparingDB
,因此这是此模块中唯一可从外部使用的内容。
如果您希望从需要的模块中获得 SQL_query
变量,您还应该导出它