gpt4 book ai didi

javascript - 错误类型错误 : subscribe is not a function

转载 作者:行者123 更新时间:2023-11-29 23:23:35 24 4
gpt4 key购买 nike

我正在尝试根据某个元素从数组中获取值,然后获取完全匹配的元素。但不知何故,我在这个函数 getProduct(name1: string) 中遇到了这个错误,我已经导入了 map 。帮助将不胜感激。this.searchProduct(...).subscribe 不是函数

 products: Product[] = [];
product: Product;

searchProduct(name2: string): Observable<Product[]> {
var config = {
headers: {
'Content-Type': 'application/json'
}
};
const ans = name2;
let obj = { name_product: ans }
let body = JSON.stringify(obj);
let header = new HttpHeaders();
header = header.append('Content-Type','application/json');
return this.httpClient.post('postRestService', body, config).subscribe(res => {
this.products = res;
console.log(this.products);
})
}

getProduct(name1: string): Observable<Product> {
return this.searchProduct(name1).subscribe(products =>
products.find(product => product.name1 == name1));
}

最佳答案

http 服务从 angular 2 更改为 angular 4。在 angular 4 中你不需要 map :

products: Product[] = [];
product: Product;

searchProduct(name2: string): Observable<Product[]> {
var config = {
headers: {
'Content-Type': 'application/json'
}
};
const ans = name2;
let obj = { name_product: ans }
let body = JSON.stringify(obj);
let header = new HttpHeaders();
header = header.append('Content-Type','application/json');
this.http.post('postRestService', body, config).subscribe(res => {
this.products = res;
console.log(this.products);
})
}

getProduct(name1: string): Observable<Product> {
return this.searchProduct(name1).subscribe(products => products.find(product => product.name1 == name1));
}

关于javascript - 错误类型错误 : subscribe is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com