相关疑难解决方法(0)

'对象'类型上不存在属性'json'

我正在尝试使用角度为2的HttpClient通过REST获取数据.我在这里按照角度教程https://angular.io/tutorial/toh-pt6并在英雄和HTTP部分下你会看到这段代码用于通过http获取英雄数据.

getHeroes(): Promise<Hero[]> {
  return this.http.get(this.heroesUrl)
         .toPromise()
         .then(response => response.json().data as Hero[])
         .catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)

以下是我在我的应用程序中编写的类似版本

fetch(startIndex: number, limit: number): Promise<Order[]> {
    let url = this.baseUrl + "&startIndex=" + startIndex + "&limit=" + limit;

    return this.http.get(url)
                .toPromise()
                .then(response => response.json().results as Order[])
                .catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用InteliJ Idea,并且在调用response.json()时有一条红线,即使我尝试使用ng build构建,我也会收到错误.

属性'json'在'Object'类型上不存在.

您可能会注意到,而不是json().datajson().results.这是因为根据教程,服务器响应了一个具有data字段的对象,但我自己的服务器使用具有results字段的对象进行响应.如果您向下滚动教程,您会看到这一点.

请注意服务器返回的数据的形状.此特定的内存中Web API示例返回具有data属性的对象.您的API可能会返回其他内容.调整代码以匹配您的Web API.

为了解决这个问题,我试过这样的事情

(response: Response) => response.json().results as Order[]
Run Code Online (Sandbox Code Playgroud)

当我这样做时,.json()方法已被解决,但另一个错误弹出

Promise类型不存在属性结果

我尝试通过定义一个接口来修复它

interface OrderResponse {
    orders: Order[]; …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular-httpclient

34
推荐指数
2
解决办法
8万
查看次数

在Typescript中使用Observable的XMLHttpRequest

当我尝试管理我上传文件的XMLHttpRequest调用的结果时,我遇到了一个tslint问题.这是我在互联网上找到的当前方法:

// Files upload request
makeFileRequest(url: string, files: Array<File>) {
    return new Promise((resolve, reject) => {
        let formData: any = new FormData()
        let xhr = new XMLHttpRequest()
        for(let file of files) {
            formData.append("uploads[]", file, file.name)
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    resolve(JSON.parse(xhr.response))
                } else {
                    reject(xhr.response)
                }
            }
        }
        xhr.open("POST", url, true)
        xhr.send(formData)
    })
}
Run Code Online (Sandbox Code Playgroud)

所以它工作,文件正在上传,后端回复200响应.但在我使用此函数的方法中,我这样做:

        this.makeFileRequest("theurl", this.listFiles)
        .map(res => res.json())
            .subscribe(
                res => console.log(res),
                error => console.log("fails")
            )
Run Code Online (Sandbox Code Playgroud)

但tslint在地图点告诉我这个: …

xmlhttprequest typescript angular

12
推荐指数
1
解决办法
2万
查看次数

在typescript/angularJs 2中的类型'JSON'上不存在属性'name'

我在angularjs 2中使用typescript语言到我的REST api应用程序,问题是 - typescript给出编译时错误

[ts] Property 'name' does not exist on type 'JSON'.any

当我尝试访问INCOMING JSON对象时,我的功能代码如下

createPerformanceChart(data: JSON) {
    // Show Up the Fund Details Data 
    var details: any;
    for(var j = 0 ; j < Object.keys(this.funds).length; j++)
    {
        if(data.name == this.funds[j].name) // data.name throws Error
        {
            details = this.funds[j];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何转换JSON或访问JSON对象 - 这样它不会抛出编译时错误并让我编译代码.

json typescript angular

1
推荐指数
1
解决办法
4266
查看次数