繁体   English   中英

在 class 中创建函数时出现错误

[英]I am getting an error when creating functions in a class

我正在尝试创建一个 class 将通过使用类与 postgres 数据库交互,但在类中创建函数时不断出错

我尝试使用带有原型的构造函数,但仍然出现错误

/* 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;

正如其他人提到的,这只是一个语法问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM