Angular 6 TypeError:无法读取未定义的属性(读取‘长度’)错误
createToken() {
this._tokenService
.getAuth(this.userId, this.token)
.subscribe(res =>
{},
error => {
console.log(error);
});
}
getAuth(upn, token) {
var param = {
Upn: upn,
Token: token
};
console.log(param);
return this._http.post(
this.REST_API_ADDR + "auth",
param,
this.getAuthRequestHeader()
);
}
getAuthRequestHeader() {
console.log("header");
const authHeaders = new HttpHeaders({
'Content-Type':'application/json',
});
return { headers: authHeaders };
}
尝试使用
getAuth
请求来存储令牌。该请求不会调用 API 并引发错误。
TypeError: Cannot read properties of undefined (reading 'length') at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.applyUpdate (http.js:199) at http.js:170 at Array.forEach () at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.init (http.js:170) at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.forEach (http.js:235) at Observable._subscribe (http.js:1435) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:42) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:28) at MapOperator.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapOperator.call (map.js:18) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:23)
您能告诉我发生此错误时要检查哪个部分吗? 另外,如果您知道错误的原因以及如何解决它,请告诉我。
此错误在此处描述和解决: https://github.com/angular/angular/issues/18743#issuecomment-367742057
基本上,您的标头具有未定义/空值。您应该检查传递给请求的标头选项。