filter(...)。toArray不是函数

时间:2017-01-03 15:10:12

标签: angular

我正在尝试过滤订阅结果。我从服务类(job.service)获取一个List,所以我过滤它,但过滤器返回一个Iterable not List或Array。所以我使用toArray()但是在运行时它会抛出一个错误,说“result.filter(...)。toArray不是一个函数”。

有人可以解释发生了什么并给我一个解决方案吗?

import { Component, Input, OnInit } from "@angular/core";
import { Http, Response, Headers, RequestOptions, RequestMethod, Request } from "@angular/http";
import { JobBase,JobMore, Employee, WorkCategory, TimeEntryApi } from "./models";
import { JobService } from "./job.service";
import { EmployeeService } from "./employee.service";
import { WorkCategoryService } from "./workcategories.service";
import { Observable } from "rxjs/Observable";
import { List } from "immutable";
import "rxjs/add/operator/debounceTime";
import "rxjs/add/operator/distinctUntilChanged";
import "rxjs/add/operator/map";
import { NgbModal, NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
dummy: Array<JobMore> = [];
getJobs(): void {
    this.jobServ.GetJobs().subscribe(
        result => this.dummy = result.filter(x => x.status > 9).toArray(),
        error => console.error,
        () => console.log('completed function here or remove '));

    this.Submitted = this.dummy.length.toString();

}

工作服务

import { Component, Injectable } from "@angular/core";
import { Http, Response, Headers, RequestOptions, RequestMethod, Request } from "@angular/http";
import { JobBase, JobMore } from "./models";
import { Observable } from "rxjs/Observable";
import { BehaviorSubject } from "rxjs";
import { List } from "immutable";
import "rxjs/add/operator/debounceTime";
import "rxjs/add/operator/distinctUntilChanged";
import "rxjs/add/operator/map";
@Injectable()
export class JobService {

    public jobs: List<JobMore>;

    constructor(public http: Http) {
        this.GetJobs();
    }

    GetJobs(): Observable<List<JobMore>>{
        return this.http.get('./api/job')
            .map(this.JobsResponseMapper);

    }

    JobsResponseMapper(response: Response) {
        this.jobs = response.json();
        return this.jobs;
    }

}

模型

export class JobMore {
    constructor(
        public id: number,
        public jobName: string,
        public jobNumber: number,
        public status: number,
    ) { }
}

ETA:

我现在尝试result => Array.from(result.filter(x => x.status > 9))我收到以下错误“类型的参数

  

Iterable'不能分配给类型的参数   '可迭代&LT; {}&GT;'。类型中缺少属性'[Symbol.iterator]'   '可迭代'。

ETA2:

我现在尝试了这个,在Array.from中使用“this.dummy”。这个语法虽然对我没有任何意义。 dummy是数组,Array.from应该将列表转换为数组并返回它。然后应该分配给假人。这有道理吗?

getJobs(): void {
    this.jobServ.GetJobs().subscribe(
        result => {
            Array.from(this.dummy = result.filter(x => x.status > 9)),
            error => console.error,
            () => console.log('jobs loaded: ' + this.jobs.length)
        });
}

我也是这样累了。具有相同的错误

getJobs(): void {
    this.jobServ.GetJobs().subscribe(
        result => {
            this.dummy = Array.from(result.filter(x => x.status > 9)),
            error => console.error,
            () => console.log('jobs loaded: ' + this.jobs.length)
        });
}
  

Iterable'不能分配给类型的参数   '可迭代&LT; {}&GT;'。类型中缺少属性'[Symbol.iterator]'   '可迭代'。

job.service

GetJobs() {
    return this.http.get('./api/job')
        .map(this.JobsResponseMapper);

}

1 个答案:

答案 0 :(得分:1)

我有点不确定您为什么要在服务中将传入数据指定为Observable,如果这不是您想要的?尝试从GetJobs()方法中移除GetJobs() { return this.http.get('./api/job') .map(this.JobsResponseMapper); } ,如下所示:

Array.from

但是作为您的问题,请尝试使用getJobs(): void { this.jobServ.GetJobs().subscribe( result => { Array.from(this.dummy = result.filter(x => x.status > 9)), error => console.error, () => console.log('completed function here or remove ') this.Submitted = this.dummy.length.toString(); }); } 和几个括号:

{{ form_start(form) }}
{{ form_widget(form) }
{{ form_end(form) }}