我试图将其放入我的其余 API 中,但我不断收到 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");
}
);
});
}
}
我调用该函数只是为了记录用户的最后登录时间,即日期以及是否成功。 通过 firebase 执行此操作,根据文档我应该收到一个 idToken:https://firebase.google.com/docs/auth/admin/verify-id-tokens
我做错了什么?
最佳答案
在这一行:
firebase.auth().currentUser.getIdToken(true).then(function(idToken) {
将匿名函数更改为箭头函数:
firebase.auth().currentUser.getIdToken(true).then((idToken) => {
这将使 this
成为函数内 LoginService
的实例。
关于javascript - 类型错误 : Cannot read property 'http' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46171359/