-2

I received a http response using nano couchdbDB but cannot use .pipe to convert the received data into what actually comes.

myservice.service.ts

export class xx{
    public mycall(docid: string = ''){
        return nanoInterface.db
            .use('mydatabase')
            .get(docid) //cannot use.pipe() after this (it does not have)
    }
}

mycontroller.controller.ts

....
    public getdoc = (doc) => {
        this.xx.mycall(doc)
            .then(results => { //type nano.DocumentGetResponse
                console.log(results); //  { _id:"xx",prop1:"xx", prop2:"xx" }
                const { _id,prop1, prop2 } = results; //works but TS gives warning: Property 'prop1' does not exist on type 'DocumentGetResponse'.ts(2339)
                const y = results.prop1; // TS gives the warning
                ...
            })
            .catch(error => {
                ...
            });

How i can access to the properties of the response object without the TS warnings? Thank you.

  • Change the `mycall` function to have a return type of `DocumentGetResponse`. – StriplingWarrior Oct 26 '22 at 23:45
  • the mycall already returns the default DocumentGetResponse but it does not corresponds to the actual data object, that is why i need to access to the actual data of the response. – Kaspacainoombro Oct 26 '22 at 23:51
  • The code you provided in your post doesn't show mycall declaring a response type. In the future, please try to provide a minimal verifiable reproduction of the issue you're encountering. – StriplingWarrior Oct 27 '22 at 04:14

1 Answers1

0

I just discovered the solution:

 public getdoc = (doc) => {
        this.xx.mycall(doc)
            .then(results => { //type nano.DocumentGetResponse
                const converted = results as MydocModel;
                const y = converted.prop1; // now TS does not complain
                ...
            })

Or...

export class xx{
    public mycall(docid: string = ''){
        return nanoInterface.db
            .use('mydatabase')
            .get(docid) 
            .then(results => results as MydocModel)
    }
}

         

Thank you.