title: Trying to fetch Data from Database. Angular 6
body: <p>Hi I am trying to fetch the data from a database to a table. I have created a <strong>service</strong> like this:</p>
  
  <pre><code>import { Injectable } from "@angular/core";
  import { HttpClient } from "@angular/common/http";
  import { Subject } from "rxjs";
  import { map } from 'rxjs/operators';
  
  import { Post } from "./post.model";
  
  @Injectable({ providedIn: "root" })
  export class PostsService {
    private posts: Post[] = [];
    private postsUpdated = new Subject&lt;Post[]&gt;();
  
    constructor(private http: HttpClient) {}
  
    getPosts() {
      this.http
        .get&lt;{ message: string; posts: any }&gt;(
          "http://localhost:3000/api/posts"
        )
        .pipe(map((postData) =&gt; {
          return postData.posts.map(post =&gt; {
            return {
              title: post.title,
              content: post.content,
              id: post._id
            };
          });
        }))
        .subscribe(transformedPosts =&gt; {
          this.posts = transformedPosts;
          this.postsUpdated.next([...this.posts]);
        });
    }
  
    getPostUpdateListener() {
      return this.postsUpdated.asObservable();
    }
  
    addPost(title: string, content: string) {
      const post: Post = { id: null, title: title, content: content };
      this.http
        .post&lt;{ message: string, postId: string }&gt;("http://localhost:3000/api/posts", post)
        .subscribe(responseData =&gt; {
          const id = responseData.postId;
          post.id = id;
          this.posts.push(post);
          this.postsUpdated.next([...this.posts]);
        });
    }
  
    deletePost(postId: string) {
      this.http.delete("http://localhost:3000/api/posts/" + postId)
        .subscribe(() =&gt; {
          const updatedPosts = this.posts.filter(post =&gt; post.id !== postId);
          this.posts = updatedPosts;
          this.postsUpdated.next([...this.posts]);
        });
    }
  }
  </code></pre>
  
  <p>When i run the code I get two different errors:</p>
  
  <p>If i use <strong>mozilla</strong> I get: </p>
  
  <blockquote>
    <p>ERROR TypeError: "_this.posts is undefined"</p>
  </blockquote>
  
  <p>If i use <strong>chrome</strong> I get:</p>
  
  <blockquote>
    <p>core.js:1542 ERROR TypeError: Cannot read property 'slice' of
    undefined
        at SafeSubscriber._next (posts.service.ts:39)</p>
  </blockquote>
  
  <p>Anybody has any idea why is this happening? Thank you</p>