0

i'm new to angular and trying to do app with date filter. So here's my problem:
I'm trying to send to do tasks to the service to recieve from another component. And when i'm running the getTasks module from my service it won't returns my array. Console shows up only the function.

import { Injectable } from '@angular/core';
import { NewTaskPageComponent, ToDoTask } from './new-task-page/new-task-page.component';
import { task } from './app.component';

@Injectable({
  providedIn: 'root'
})
export class TaskService {
  tasks:ToDoTask[]=[];
  constructor() { }
  addTask(task:ToDoTask){
    this.tasks.push(task);
  }
  getTasks():ToDoTask[]{
    return this.tasks;
  }

}


this is my service.ts

import {AppComponent} from '../app.component';
import { Component, OnInit } from '@angular/core';
import { TaskService } from '../task.service';

@Component({
  selector: 'app-new-task-page',
  templateUrl: './new-task-page.component.html',
  styleUrls: ['./new-task-page.component.sass'],
  providers: [TaskService]
})
export class NewTaskPageComponent implements OnInit {
  newTask ?: ToDoTask;
  constructor(private _taskService:TaskService){

  }

  ngOnInit(): void {
  }
  sendTask(task:string,date:string): void{
    this.newTask = new ToDoTask(task,date);
    this._taskService.addTask(this.newTask);
    console.log(this._taskService.getTasks);
  }

}
export class ToDoTask{
  task: string;
  date: string;
  constructor(task:string,date:string){
    this.task = task;
    this.date = date;
  }

}

and this is my component which i'm trying to send and get the data.

1
  • 1
    Replace console.log(this._taskService.getTasks) with console.log(this._taskService.getTasks()). Since getTasks is the method of the service you need to call it. Commented Jul 6, 2021 at 9:44

1 Answer 1

0

Thanks to a comment by vadimk7, I solved the problem by adding parentheses around the function.

I replaced console.log(this._taskService.getTasks) in my original code with console.log(this._taskService.getTasks()).

The trailing parentheses are necessary because getTasks is a method that is being called on the service.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.