【发布时间】:2019-01-13 04:17:03
【问题描述】:
我正在调用 API 端点并收到错误返回,但在使用 console.log 时我无法查看错误,因为我在下面收到错误。有没有其他方法可以得到错误?
错误
TypeError: Cannot read property 'length' of null
at http.js:109
at Array.forEach (<anonymous>)
at HttpHeaders.lazyInit (http.js:103)
at HttpHeaders.init (http.js:251)
at HttpHeaders.forEach (http.js:354)
at Observable._subscribe (http.js:2153)
at Observable._trySubscribe (Observable.js:172)
at Observable.subscribe (Observable.js:160)
at Object.subscribeToResult (subscribeToResult.js:23)
at MergeMapSubscriber._innerSub (mergeMap.js:132)
提供者
import { HttpClient } from '@angular/common/http';
import { Http, Headers , RequestOptions } from '@angular/http';
import { Injectable } from '@angular/core';
@Injectable()
export class GamesProvider {
// API url
apiUrl = 'http://xxxxx.com/api';
headers : any;
options: any;
this.headers = new Headers();
this.headers.append('Accept', 'application/json');
this.headers.append('Content-Type', 'application/json' );
this.headers.append('Authorization', 'Bearer' + this.token);
this.options = new RequestOptions({headers: this.headers});
getUsers(ex) {
return new Promise(resolve => {
this.http.get(this.apiUrl+'/api/online/'+ex, {headers: this.options}).subscribe(data => {
resolve(data);
}, err => {
console.log(err);
}).catch((err) => {
// Do messaging and error handling here
return Observable.throw(err)
});
});
}
【问题讨论】:
-
你在哪里使用长度?我认为您错过了使用长度属性的部分
-
我不使用它...
-
这是一个函数吗?你能展示更多你的代码吗?
-
是的,它是 1 个函数的全部代码
-
return new Promise((resolve,reject) =>andreject(err);inside your catch 或err=>{ reject(err);}我不确定它是否会给出相同的结果
标签: angular