【问题标题】:error TS2339: Property 'results' does not exist on type 'Response'错误 TS2339:“响应”类型上不存在属性“结果”
【发布时间】:2016-08-19 00:24:23
【问题描述】:

我遵循了一段代码构建和应用程序,它使用 angular2、typescript 和 firebase 从 last.fm 收集数据。

源代码可以在这里找到:https://github.com/vtts/mytunes

问题如何转换 json 调用的结果?

github上的源码

https://github.com/vtts/mytunes/blob/master/app/music/services/music.srv.ts

我收到以下错误:

app/music/services/music.service.ts(29,39): error TS2339: Property 'results' does not exist on type 'Response'. 
app/music/services/music.service.ts(51,36): error TS2339: Property 'album' does not exist on type 'Response'.

package.json

{
  "name": "mytunes",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.15",
    "bootstrap": "^3.3.6",
    "es6-shim": "^0.35.0",
    "firebase": "^2.4.2",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "systemjs": "0.19.26",
    "zone.js": "0.6.10"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.2.0",
    "typescript": "^1.8.10",
    "typings": "^0.7.12"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "outDir": "js"
    },
    "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]
}

Last.fm REST API

我正在使用以下 REST API:

【问题讨论】:

  • 看起来问题出在这一行 github.com/vtts/mytunes/blob/… 尝试将 res 打印到控制台并检查它是否真的有 results 键。
  • 亲爱的@GünterZöchbauer,我什至无法运行应用程序,因为编译失败

标签: json firebase angular last.fm typescript1.8


【解决方案1】:

而不是重用变量res,它被键入为Response

res = res.json();

新建一个

let result = res.json();

然后在下面的行中使用result 而不是res

【讨论】:

  • 亲爱的@GünterZöchbauer,您的提案已经在第 29 行中到位,同样的问题也发生了。
  • 然后删除第 27 行并将 29 放在它的位置。
  • 显然,你是对的,我调试这段代码太久了。非常感谢
猜你喜欢
  • 2020-03-25
  • 1970-01-01
  • 2019-01-21
  • 2016-08-13
  • 2017-12-20
  • 1970-01-01
  • 2016-11-14
  • 2017-10-24
  • 2021-08-10
相关资源
最近更新 更多