undefined - 为什么在浏览器中显示订阅未定义
问题描述
updload(name,age,address)
{
debugger;
this.data=this.service.tryinsert(name,age,address);
this.data.subscribe(
(respose:any)=>{
console.log(respose);
}
)
}
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
我已经导入了上述两个文件,但是当我从浏览器传递姓名、年龄、地址的数据时,它说
错误类型错误:无法读取未定义的属性“订阅”
解决方案
问题说
错误类型错误:无法读取未定义的属性“订阅”
因为到this.data.subscribe()
被调用的时候,变量this.data
还没有被赋值。
服务函数几乎总是在角度上是可观察的,因此是异步的。这意味着服务器的响应将取决于几个因素,而不是瞬时的。
要更正您的代码,您可以像下面这样使用它:
updload(name,age,address)
{
debugger;
this.service.tryinsert(name,age,address)
.subscribe(responseData => {
console.log(responseData);
// use the code below if responseData is not of type string
// console.log(JSON.stringify(responseData));
});
}
推荐阅读
- python-3.x - 如何从 ubuntu 终端中运行的 python 脚本导航?
- rust - 使用quote crate时有没有办法去掉引号?
- snowflake-cloud-data-platform - 有哪些可用的雪花版本?
- css - 嵌套网格延伸到视图的末端
- config - 使用配置触发 Airflow DAG 的理想方式
- amazon-web-services - 如何使用 cloudwatch 代理监控文件夹大小
- c++ - 使用指针向右移动数组元素
- javascript - jQuery 使用每个函数调用存储和执行以前的 AJAX HTTP 请求
- php - 执行“php artisan key:generate”时出现错误“无法加载动态库'mcrypt'”
- java - java通过排除元素将一个列表拆分为多个列表