Javascript 在类中创建函数时出错
我试图创建一个类,该类将通过使用类与postgres数据库交互,但在类中创建函数时不断出错 我曾尝试将构造函数与原型一起使用,但仍然出现错误Javascript 在类中创建函数时出错,javascript,node.js,Javascript,Node.js,我试图创建一个类,该类将通过使用类与postgres数据库交互,但在类中创建函数时不断出错 我曾尝试将构造函数与原型一起使用,但仍然出现错误 /* eslint linebreak-style: ["error", "windows"] */ // eslint-disable-next-line no-unused-vars const pool = require('../config/config.js'); // user constructor class User { const
/* eslint linebreak-style: ["error", "windows"] */
// eslint-disable-next-line no-unused-vars
const pool = require('../config/config.js');
// user constructor
class User {
constructor(user) {
this.username = user.username;
this.email = user.email;
this.password = user.password;
this.role = user.role;
}
// save new user in databes
function createUser() {
}
}
module.exports = User;
function createUser(params) {
^^^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
这就是我得到的错误
const pool = require('../config/config.js');
// user constructor
class User {
constructor(user) {
this.username = user.username;
this.email = user.email;
this.password = user.password;
this.role = user.role;
}
// save new user in database
createUser() {
}
}
module.exports = User;
正如其他人提到的,这只是一个语法问题 在javascript类中,不使用关键字
函数
。只需createUser(){your code}
或async createUser(){…}
等