TypeError:无法读取未定义的属性“管道”

内存

我正在用RxJS在Angular中编写实时搜索功能。我遇到一些错误,因为TypeError:无法读取未定义的属性“管道”。我正在使用Angular 7,并且尝试了StackOverfloew的不同代码示例,但无法解决此问题。

app.Component.html

<input type='text' class="form-control input-txt-start" placeholder="Search Domain Name" name="domainId" (keyup)='getSearchResults(searchTerm$.next($event.target.value))'>

<ul *ngIf="results">
    <li *ngFor="let result of results | slice:0:9">
        {{ result}}
    </li>
</ul>
<p *ngIf="error">
    {{error}}
</p>

app.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators, NgForm, FormControl } from '@angular/forms';
import { SearchService } from 'src/app/services/search.service';
import { Subject } from 'rxjs';

@Component({
  ...
  providers: [SearchService]
})

export class AppComponent implements OnInit { 
  results: Object;
  searchTerm$: any = new Subject();
  error: any;

  constructor(private searchService: SearchService) { }

  ngOnit() { }

  getSearchResults(search) {
    this.searchService.search(search).subscribe((res) => {
      console.log(res);
      this.results = res;
    }, err => {
      console.log(err);
      this.error = err.message;
    });
  }
}

search.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { distinctUntilChanged } from 'rxjs/operators';
import { map } from 'rxjs/operators';
import { switchMap } from 'rxjs/operators';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root'
})

export class SearchService {
  public httpOptions = {
    headers: new HttpHeaders({'Content-Type': 'application/json'})
  };

  baseUrl: String = `${environment.API_URL}/api/domainCharts`;
  queryUrl: String = '?search=';

  constructor( private http: HttpClient ) { }

  search(terms: Observable<string>) {
    return terms.pipe(debounceTime(500)).pipe(distinctUntilChanged()).pipe(switchMap(term => this.searchEntries(term)));
  }

  searchEntries(term) {
    return this.http.get(`${this.baseUrl}${this.queryUrl}${term}`);
  }
}
哈伦·伊尔马兹(Harun Yilmaz)

有不止一件事情与您的要求不符。

首先,您传递了Subject并期望Observable这就是为什么你会出错

TypeError:无法读取未定义的属性“管道”

然后,您传递Subjectterm(我假设您要发送搜索关键字)。

Subject在您的情况下,您不需要您可以这样做:

模板:

<input type='text' class="form-control input-txt-start" placeholder="Search Domain Name" name="domainId" (keyup)='getSearchResults($event)'>  <---- Send only $event here

零件:

getSearchResults(event) {
    this.searchService.search(event.target.value).subscribe((res) => { // <--- Get target value of event
      console.log(res);
      this.results = res;
    }, err => {
      console.log(err);
      this.error = err.message;
    });
  }
}

服务:

search(term) {  // <--- Notice that the parameter name became term instead of terms

    // You can use of() method of RxJS
    // You need to import of with "import { of } from 'rxjs';"
    // You can chain you operators in pipe() method with commas

    return of(term).pipe(
       debounceTime(500),
       distinctUntilChanged(),
       switchMap(() => this.searchEntries(term) // <--- Notice that, this function has no parameter
     );
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:无法读取未定义的属性“ then”

未捕获的TypeError:无法读取未定义的属性“ toLowerCase”

未捕获的TypeError:无法读取未定义的属性“值”

“ TypeError:无法读取未定义的属性'then'

Uncaught TypeError:无法读取未定义的属性“轴”

TypeError:无法读取未定义的属性“数据”-但已定义

TypeError:无法读取未定义的属性“ map”

Angular 4管道无法读取未定义的属性“ toLowerCase”

无法读取节点js中未定义的属性“管道”

TypeError:无法读取未定义的属性“编译”

无法读取DashboardComponent上未定义的属性“管道”

刷卡器:TypeError:无法读取未定义的属性“ push”或TypeError:classNames未定义

Formik-TypeError:无法读取未定义的属性'type'?

TypeError:无法读取未定义的属性“未定义”

Uncaught TypeError:无法使用数据表管道读取未定义的属性“ style”

“ TypeError:无法读取未定义的属性”

如何修复'TypeError:无法读取未定义的属性'then'TypeError:无法读取未定义的属性'then'...'

未捕获的TypeError:无法读取未定义的属性“ close”

UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“body”

ReactJS TypeError:无法读取未定义的属性(读取“地图”)

反应 - 错误:TypeError:无法读取未定义的属性(读取“then”)

“TypeError:无法读取未定义的属性(读取'id')”

TypeError:无法读取未定义的属性(读取“有”)

Jasmine 单元测试 - TypeError:无法读取未定义的属性(读取“管道”)

TypeError:无法读取未定义的属性(读取“等于”)

React - TypeError:无法读取未定义的属性(读取“参数”)?

React TypeError:无法读取未定义的属性(读取“状态”)

TypeError:无法读取未定义的属性(读取“pageNumber”)

“TypeError:无法读取未定义的属性(读取'hasOwnProperty')”错误