Node.js和ES6类错误

时间:2018-08-05 17:58:30

标签: javascript node.js ecmascript-6

在我的小型编码场所中有一个小错误,经过数小时的努力,我仍然找不到解决错误的方法。

我只是想在app.js文件中创建Post类的对象(从另一个Post.js文件中需要)。 (请参见第一个片段)

这是代码和错误。

---下面的app.js文件

'use strict';
const bodyParser = require('body-parser'),
mysql            = require('mysql'),
express          = require('express'),
app              = express(),
Post             = require('./Post.js');

app.get('/getposts', (req,res) => {
let sql = 'select * from posts'
let query = db.query(sql, (err,results) => {
    if(err) throw err;

    let id = results[0].id;
    let title = results[0].title;
    let body = results[0].body;

    var post = new Post(id,title,body);


    res.send('BLAH');
});
});

-下面的Post.js文件

'use strict';


class Post {

constructor(id,title,body) {
    this.id = 'id';
    this.title = 'title';
    this.body = 'body';
}

get id() {
    return this.id;
}

set id(id) {
    this.id = id;
}

get title() {
    return this.title;
}

set title(title) {
    this.title = title;
}

get body() {
    return this.body;
}

set body(body) {
    this.body = body;
}

write() {
    return `${this.id} that is the id, the title is ${this.title} and the body is : ${this.body}`
}
}

module.exports = Post;

-错误

C:\Users\skor\Desktop\app\webbootcamp\Post.js:16
set id(id) {
      ^
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:16:11)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)
at Post.set id [as id] (C:\Users\skor\Desktop\app\webbootcamp\Post.js:17:17)

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

您返回并设置get id() { return this.id; } set id(id) { this.id = id; } 的实际gettersetter,而不是更改classget id() { return this._id; } set id(id) { this._id = id; } 属性,因此:

class Post {
  constructor(id, title, body) {
    this._id = id;
    this._title = title;
    this._body = body;
  }

  get id() {
    return this._id;
  }

  set id(id) {
    this._id = id;
  }

  get title() {
    return this._title;
  }

  set title(title) {
    this._title = title;
  }

  get body() {
    return this._body;
  }

  set body(body) {
    this._body = body;
  }

  write() {
    return `${this._id} that is the id, the title is ${this._title} and the body is : ${this._body}`
  }
}

将代码更改为此:

constructor

还可以像这样在整个课程中更改您的吸气剂和吸气剂:

this.id = 'id';

还要确保在this._id = id; 中,当您设置属性以使用值而不是字符串时,例如:代替:

public

使用如下:

private

使用前缀来区分语言中的_id属性是一种常见的约定,但是由于JavaScript没有(yet)私有属性,因此这种通用技术用于获得类似的结果,例如:和在this._id前加({class Point { constructor(x, y) { this._x = x; this._y = y; } } ,如下:class Person { constructor(name, age) { this.getAge = function () { return age; } this.setAge = function (newAge) { age = newAge; } this.getName = function () { return name; } this.setName = function (newName) { name = newName; } } } ,即:

/** @type {WeakMap<Person, {name: string, age: number}>} */
const internal = new WeakMap();

class Person {
  constructor(name, age) {
    internal.set(this, {name, age});
  }

  get age() {
    return internal.get(this).age;
  }

  set age(val) {
    internal.get(this).age = val;
  }
  // and the same for name
}

有关此技术的有用资源:


this blog中所述,您应该了解的另外两种在JS中实现私有成员的技术是close&WeakMap。三种不同方法中的每一种都有其优点和缺点。 简短示例:

type Type int

const (
    A Type = iota
    B
    C
)

var typeToString = map[Type]string{
    A: "A",
    B: "B",
    C: "C",
}

func (t Type) MarshalJSON() ([]byte, error) {
    return json.Marshal(typeToString[t])
}

type Character struct {
    Type Type `json:"type"`
}

func main() {
    c := Character{}
    c.Type = A
    j, err := json.Marshal(c)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(j))
}

优势:

  • 私有成员的简单实现,而无需外部访问私有成员。

缺点:

  • 此类的每个对象都有其自己的功能,并且它们不像前缀解决方案那样在原型上共享它们。

  • 成员太多无法访问-同一类的对象无法访问彼此的私有成员,这不是私有成员的标准实现。

WeakMap的简短示例:

MarshalJSON

优势:

  • 原型上的共享功能。

  • 完全私有实现(同一类的对象可以访问彼此的私有成员

缺点:

  • 不是简单的实现。

通常,WeakMap是最好的方法,但并非总是如此。

答案 1 :(得分:2)

只要设置了对象的属性,就会调用您的setter。构造函数调用设置器。然后,因为设置器执行以下操作:

this.id = id;

它将以递归方式调用自身,因此由于堆栈会溢出而产生错误。一个解决方案是删除您的setter和getter,而仅通过以下方式获取和设置属性:

class Post {

constructor(id,title,body) {
    this.id = 'id';
    this.title = 'title';
    this.body = 'body';
}


write() {
    return `${this.id} that is the id, the title is ${this.title} and the body is : ${this.body}`
}
}

let hey = new Post(1,'foo','bar');

// set something like this:
hey.id = 5;

// get something like this:

const id = hey.id;

答案 2 :(得分:1)

我认为您的代码存在问题。

get id() {
    return this._id;
}

set id(id) {
    this._id = id;
}

我对您的代码所做的更改是将_放在属性名称之前。