angular - 未捕获(承诺):TypeError:无法读取 angular2+ 中未定义的属性“get”
问题描述
我在 angular2+ 文件中创建了一个组件。我想向 nodeJs 服务器发出一个 http 请求。但是,我不断得到->
Uncaught (in promise): TypeError: Cannot read property 'get' of undefined
TypeError: Cannot read property 'get' of undefined
需要注意的是,我什至无法检索存储在一个函数中并且(无法)在另一个函数中访问的全局变量的值。这是我第一次面对它。
代码 -
app.component.ts
import { HttpClient, HttpParams, HttpRequest, HttpEvent } from '@angular/common/http';
@Injectable()
export class MachineMergerComponent implements OnInit {
constructor(private http: HttpClient) {}
onFileLoadTopFive(fileLoadedEvent) {
// -----=================== Top Five Neovis ==================== -------------
this.rhsNameTopFive = [];
this.relationshipInfo = [];
for (var i = 0; i < 5; i++) {
this.rhsNameTopFive.push(this.objectValueAfterConfidence1[i].rhs[0])
console.log("this.rhsNameTopFive = ", this.rhsNameTopFive)
// http request
var relationObj = {
'relationObj1': this.rhsNameTopFive[i]
}
}
console.log("relationObj = ", relationObj);
this.http.get("http://localhost:3003/seekRelationship?relationObj=" + this.rhsNameTopFive[0])
.map(Response => Response)
.catch((err) => {
console.log("err =", err)
return Observable.throw(err);
})
.subscribe((res: Response) => {
console.log("XXXXXXXXXXXX Response on /seekExtraction", res);
this.relationshipInfo[i] = res;
console.log(" this.relationshipInfo[" + i + "]", this.relationshipInfo[i])
})
}
}
app.module.ts
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
CommonModule,
FormsModule,
// HttpModule ,
HttpClientModule,
],
declarations: [ MachineMergerComponent, ]
})
export class ApiSequenceModule { }
解决方案
MachineMergerComponent 是一项服务。将其添加到提供者列表下。(也可以考虑将其重命名为 MachineMergerService )
declarations: [ ... ],
providers: [ MachineMergerComponent ]
推荐阅读
- loops - 循环 2 个选择选项并显示 API 最佳匹配
- amazon-web-services - 如何从 AWS SES 中删除经过验证的域?
- macos - 为什么 macOS 上的动态库有 id?
- video - 当嵌入的视频处于自动播放状态时,youtube 是否会计算观看次数?
- javascript - Width attribute not acting properly on my webpage
- python - 如何基于组加入 Pyspark 数据帧
- javascript - d3.js 多线图上的点(符号)颜色
- webview - 您无法从此屏幕登录,因为此应用不符合 Google 的嵌入式网页浏览政策
- java - 有没有办法解决 Intellij 中的这个内部错误
- html - 无法弄清楚如何为网站正确覆盖带有图像的 div