0

im trying to put this in my rest API, but i keep getting the TypeError.

import { Component, OnInit, Inject, Injectable } from '@angular/core';
import { AuthService } from "../core/auth.service";
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import * as firebase from 'firebase/app'; //Firebase SDK

@Injectable()
export class LoginService implements OnInit {

  constructor(public auth: AuthService, private http: HttpClient) {

  }
  ngOnInit() {
  }

  lastLogin() {
    firebase.auth().currentUser.getIdToken(true).then(function(idToken) {
      // Send token to your backend via HTTPS
      //console.log(idToken);
      const req = this.http.put('https://dev-api.byrd.news/v1/login', {
        user_token: idToken
      })
      .subscribe(
        res => {
          console.log(res);
        },
        err => {
          console.log("error skete");
        }
      );
    });

  }
}

I call the function just for logging the lastLogin of a user, which is date and if it were a succes. Doing it through firebase, and i should receive an idToken according to the docs: https://firebase.google.com/docs/auth/admin/verify-id-tokens

What am i doing wrong?

eko
  • 39,722
  • 10
  • 72
  • 98
blixenkrone
  • 388
  • 2
  • 7
  • 20

1 Answers1

1

On this line:

firebase.auth().currentUser.getIdToken(true).then(function(idToken) {

Change the anonymous function to an Arrow-function:

firebase.auth().currentUser.getIdToken(true).then((idToken) => {

This will let this be the instance of LoginService inside the function.

Arg0n
  • 8,283
  • 2
  • 21
  • 38
  • Thank you for this! I'm too caught up with the documentation apparently - i've changed it to fat arrow, i'll report back with the results (big database). – blixenkrone Sep 12 '17 at 08:41