Access to Angular service data 5

1

Why does it only allow me to access the data in a specific way?

I have a service:

getCvs() : Observable<Array<Cv>>{
    return this._http.get<Array<Cv>>(this.url + 'getCvs').pipe(
        tap(cvs => this.log('Fetched CVs'))
    );
}

Which returns an array of Cv. The fact is that it only lets me access the answer through the function of the component in this way:

getCvs(){
    this._cvService.getCvs().subscribe(response => {
        this.cvs = response['cvs'];
    });
}

And this is the API method:

function getCvs(req, res){
    if (req.params['id']) {
        var id = req.params['id'];
        Cv.findById(id, (err, cv) => {
            if (err) { res.send({message: 'Error en el servidor.'}); }
            return res.send({message: 'Cv:', cv:cv});       
        });
    }else{
        Cv.find({}, (err, cvs) => {
            if (err) { res.send({message: 'Error en el servidor.'}); }
            return res.send({cvs:cvs});
        });
    }
}

The fact is that if I in the corner part I put:

getCvs(){
        this._cvService.getCvs().subscribe(response => {
            this.cvs = response.cvs; <---esto cambio
        });
    }

I also receive the data, but angular gives me an error that cvs does not exist in type Cv [];

Why can I only access in this way without angular 5 error?

I have been looking at the documentation of angular 5 of HttpClient, etc. And I have not found the solution. Even in the documentation there are the examples on which I have based and I have not had that kind of problem. Thanks.

    
asked by H. Díaz 01.02.2018 в 12:22
source

0 answers