title: How to initialise a model and call a custom function within the class
body: <p>I have a user service where i get a object from my mongo db. The user object contains a dob which i want to convert to an age in angular.</p>
  
  <p>I created a custom user class to do this declaring a getAge function. However when calling this user.getAge() function it says </p>
  
  <blockquote>
    <p>"this.user.getAge() is not a function"</p>
  </blockquote>
  
  <p>user.model.ts</p>
  
  <pre><code>export class User {
  
    user: any;
  
    constructor() {
  
    }
  
    getAge() {
  
  return "Age"
  
    }
  }
  </code></pre>
  
  <p>profile.component.ts</p>
  
  <pre><code>import { UserService } from '../../services/user.service';
  import { AuthService } from '../../services/auth.service';
  import { Component, OnInit } from '@angular/core';
  import { Router, ActivatedRoute, ParamMap } from '@angular/router';
  import 'rxjs/add/operator/switchMap';
  import {User} from '../../models/user.model'
  
  @Component({
      selector: 'app-profile',
      templateUrl: './profile.component.html',
      styleUrls: ['./profile.component.scss']
  })
  export class ProfileComponent implements OnInit {
  
      user: User;
  
      constructor(private authService: AuthService,
          private route: ActivatedRoute,
          private router: Router,
          private userService: UserService) { }
  
      ngOnInit() {
  
          this.route.params.switchMap((params) =&gt; {
              let user_id = params['id'];
              return this.userService.get(user_id);
          }).subscribe((res) =&gt; {
              this.user = res;
              let age = this.user.getAge();
              console.log(age);
  
          });
      }
  
  }
  </code></pre>
  
  <p>user.service.ts</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { ApiService } from './api.service';
  
  @Injectable()
  export class UserService {
  
  
      path = 'users/';
  
      constructor(
          private apiService: ApiService
      ) {
  
      }
  
      all() {
          return this.apiService.get(this.path);
      }
  
      create(user) {
  
          return this.apiService.post(this.path, user);
  
      }
  
      get(user_id) {
  
          let endpoint = this.path + user_id;
  
          return this.apiService.get(endpoint);
      }
  
  }
  </code></pre>
  
  <p>user.model.js</p>
  
  <pre><code>let UserSchema = new Schema({
  
          email: {
              address: {
                  type: String,
                  lowercase: true,
                  //unique: true,
  
              },
              token: String,
              verified: {
                  type: Boolean,
                  default: false,
              },
          },
          password: {
              type: String,
          },
  
          socketId: String,
          isOnline: Boolean,
  
          phone: {
              countryCode: {
                  type: String,
              },
              number: {
                  type: String,
              },
              code: String,
              verified: {
                  type: Boolean,
                  default: false
              },
          },
  
          jwt: String,
  
          profile: {
              username: String,
              firstname: String,
              lastname: String,
              dob: String,
              gender: String,
              level: Number,
              location: {
                  longitude: String,
                  latitude: String
              },
              image: String,
              introduction: String,
  
              following: [],
              followers: [],
  
          },
  
      },
      {
          timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
      });
  </code></pre>