JavaScript Node.js and ES6 class error node error class is not a constructor,node js error

I have a small bug in my small coding playground and I just can't find any solution to my error after hours of struggling.

I simply want to create an object of a Post class in a app.js file (required from another Post.js file). (see first snippet)

Here's the code and the error.

--- app.js file below

'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 file below

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

--Error

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)

Thanks so much for help!

Answer:1

Your setter is called whenever a property of the object is set. The constructor calls the setter. Then because the setter does the following:

this.id = id;

It will call itself recursively and thus it gives an error because the stack will overflow. An solution is to remove your setters and getters and just get and set your properties in the following manner:

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;
Answer:2

I've a problem with my App, normally , ng-class displays a different background according to the time indicated in the database. It works fine on other views ( Home, and when creating ), but in the ...

I've a problem with my App, normally , ng-class displays a different background according to the time indicated in the database. It works fine on other views ( Home, and when creating ), but in the ...

  1. angularjs choose class

Let's say I have this string: <i id="1"></i><i id="2"></i><i id="3"></i> and this object: { "1": "Red", "2": "Green", "3": "Blue" } Then I want to obtain ...

Let's say I have this string: <i id="1"></i><i id="2"></i><i id="3"></i> and this object: { "1": "Red", "2": "Green", "3": "Blue" } Then I want to obtain ...

  1. string replace with regex
  2. string replace with regex python
  3. string replace with empty
  4. string replace with regex java
  5. string replace with regex javascript
  6. string replace with dictionary python
  7. string replace with python
  8. string replace with regex c#
  9. string replace with regex js
  10. string replace with empty char java
  11. string replace with empty char c#
  12. string replace with variable python
  13. string replace with multiple values
  14. string replace with space
  15. string replace with index in java
  16. string replace with java
  17. string replace with index
  18. string replace with variable javascript
  19. string replace with null java
  20. string replace with space java

I am trying to toggle plus and minus span and also toggle the answer div all at once and its not working out. I built the html so the question div has two childrens: div icons with two children of ...

I am trying to toggle plus and minus span and also toggle the answer div all at once and its not working out. I built the html so the question div has two childrens: div icons with two children of ...

  1. jquery toggle class onclick
  2. jquery toggle class animation
  3. jquery toggleclass not working
  4. jquery toggle class on hover
  5. jquery toggle class multiple
  6. jquery toggle class based on boolean
  7. jquery toggle class on button click
  8. jquery toggleclass delay
  9. jquery toggle class hide show
  10. jquery toggle class codepen
  11. jquery toggle class on scroll
  12. jquery toggle class condition
  13. jquery toggle class function
  14. jquery toggle class click
  15. jquery toggle class callback
  16. jquery toggle class by id
  17. jquery toggleclass off
  18. jquery toggle class visibility
  19. jquery toggle class this
  20. jquery toggle class on focus

I am a beginner with typescript and playing with it on their Playground site. I have the following code: class Queue<T> { private data = []; push = (item: T) => this.data.push(item)...

I am a beginner with typescript and playing with it on their Playground site. I have the following code: class Queue<T> { private data = []; push = (item: T) => this.data.push(item)...

  1. typescript doesn't compile
  2. typescript doesn't compile on save
  3. typescript doesn't work
  4. typescript doesn't understand
  5. typescript does not like
  6. typescript import doesn't work
  7. typescript instanceof doesn't work
  8. typescript exclude doesn't work
  9. typescript concat doesn't work
  10. typescript await doesn't work
  11. typescript filter doesn't work
  12. typescript await doesn't wait
  13. typescript foreach doesn't work
  14. typescript getter doesn't work
  15. typescript regex doesn't work
  16. typescript settimeout doesn't work
  17. typescript object doesn't support this action
  18. typescript includes does not work
  19. typescript typings doesn't work
  20. typescript property does not exists