簡體   English   中英

Node.js和ES6類錯誤

[英]Node.js and ES6 class error

我在小型編碼場所中遇到一個小錯誤,經過數小時的努力,我仍然找不到解決我錯誤的方法。

我只想在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)

非常感謝您的幫助!

您返回並設置id的實際gettersetter ,而不是更改classid屬性,因此:

get id() {
    return this.id;
}

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

將代碼更改為此:

get id() {
    return this._id;
}

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

並在整個班級中更改您的getter和setter方法,如下所示:

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;

它的使用前綴來區分共同約定publicprivate的語言特性,但由於JavaScript沒有(還)私人性質,這種常見的技術用於實現類似的結果,如:和下划線_之前id ,如所以: this._id ,即:

class Point {
  constructor(x, y) {
    this._x = x;
    this._y = y;
  }  
}

關於此技術的有用資源:


本博客所述 ,您還應該注意的另外兩種在JS中實現私有成員的技術是close&WeakMap。 三種不同方法中的每一種都有其自身的優點和缺點。 簡短示例:

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; }
  }
}

優點:

  • 私有成員的簡單實現,而無需外部訪問私有成員。

缺點:

  • 此類的每個對象都有其自己的功能,並且它們不像前綴解決方案那樣在原型上共享它們。

  • 成員太多無法訪問-相同類的對象無法訪問彼此的私有成員,這不是私有成員的標准實現。

WeakMap的簡短示例:

/** @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
}

好處:

  • 原型上的共享功能。

  • 完全私有的實現(同一類的對象可以訪問彼此的私有成員

缺點:

  • 這不是一個簡單的實現。

通常,WeakMap是最好的方法,但並非總是如此。

只要設置了對象的屬性,就會調用您的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; 

我認為您的代碼的問題是這樣的。

get id() {
    return this._id;
}

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

我在您的代碼中所做的更改是將_放在屬性名稱之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM