mysql - Node.js、Express 和 MySQL。要求未定义
问题描述
我在我的一个项目中使用 node.js、express 和 mysql,但是当我调用 mysql 时,它向我显示一个错误,告诉我“[Error] ReferenceError: Can't find variable: require”。
//server.js
class SQL{
constructor(){
var mysql = require('mysql');
this.connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'newDB'
});
}
connect(){
this.connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('Connected');
});
}
disconnect(){
this.connection.end(function(err) {
if (err) {
console.error('error ending connection: ' + err.stack);
return;
}
console.log('Connection ended');
});
}
}
// main.js
function main(){
var database = new SQL();
}
main();
在安装 express 之前它工作正常,但在我安装 express 之后它显示错误。
类和函数位于不同的文件中。
解决方案
推荐阅读
- keras - 如何处理 Keras 中的海量数据以避免 MemoryError?
- azure-devops - Azure DevOps 管道装饰器不会注入自定义任务,除非后者手动添加一次
- windows - 创建商店。不会正确减去钱
- c++ - 我编写了一个程序来管理学生数据和重载运算符,但我得到了一个错误 free(): invalid pointer
- python - 通过 Python 从 Impala 访问表
- python - 当我们在垂直线上有数据时如何获得最佳拟合线?
- android - 如何在颤动中阅读收到的通知
- node.js - 将 blob 存储在 blob 容器中的文件夹中
- angular - IntelliJ 中的角度问题
- php - Laravel Eloquent 返回错误的 created_at 和 updated_at 值