类型“ OperatorFunction <未知,任何>”上不存在属性“订阅”

时间:2019-06-28 22:10:50

标签: angular

我正在尝试从 mongodb 获取一些数据,并不断收到此错误:

  

类型“ OperatorFunction”上不存在属性“订阅”

// app.component.ts

import { Component } from '@angular/core';
import {FormGroup, FormControl, Validators, FormsModule} from '@angular/forms';
import {CommonService} from './common.service';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';
import { from } from 'rxjs';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {

    constructor(private newService: CommonService) {
    }

    Repdata;
    valbutton = "Save";

    ngOnInit() {
        this.newService.GetProcesses().subscribe(data => this.Repdata = data);
    }
}

// common.service.ts


import { Injectable } from '@angular/core';
import {Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { map, tap } from 'rxjs/operators';
import 'rxjs/add/operator/do';
import 'rxjs/Rx';
import { catchError } from 'rxjs/operators';

@Injectable()
export class CommonService {

    constructor(private http: Http) { }

    GetProcesses(){
        return this.http.get('http://localhost:4000/getProcesses/')
                   .pipe(map((response: Response) => response.json())),
            catchError(error => Observable.of(null))

    }
}

类型“ OperatorFunction”上不存在属性“订阅”

1 个答案:

答案 0 :(得分:-1)

似乎您的common.service.ts文件中的括号位置错误。

我为您制作了此代码段,以供您尝试,希望它能满足您的需求

// common.service.ts

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {catchError, map} from 'rxjs/operators';
import 'rxjs/add/operator/do';
import 'rxjs/Rx';

@Injectable()
export class CommonService {

    constructor(private http: Http) {
    }

    GetProcesses() {
        let url = 'http://localhost:4000/getProcesses/';
        return this.http.get(url).pipe(
            map((response: Response) => response.json()),
            catchError(error => Observable.of(null))
        );
    }
}