Node.js I cannot access a class property in class method

Asked

Viewed 130 times

1

Fala galera,

this starting to study Node.js, I’m doing an exercise here I’m encountering the following problem:

inserir a descrição da imagem aqui

In this case, I am not being able to access this.Ojects property, which was started in the constructor method, in the store method itself of the class.

Can anyone tell me what I’m doing wrong?

  • Do not enter your code as image. Place it as text in the body of your question.

2 answers

0

Brother a solution would be you pass projects as a parameter for the constructor, follow an example:

class ProjectController {
  constructor(projects) {
    this.projects = projects;
  }

  store(req, res) {
    return this.projects;
  }
}

console.log(new ProjectController([]));

-1

You need to create the variable first and then access it with this.


class TODOLIST {

public todo: [] = [];

constructor() {

 this.todo = ['task 1'];

}


 public addTodo(): void {
   this.todo.push(['task 2']);

 }

}
  • 1

    This is Typescript, in Javascript the properties can be declared and initialized dynamically within the constructor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.