LaCodeM
LaCodeM

Reputation: 829

Soap call with NestJS

I have to call SOAP webservice and serve it. I am using strong-soap library(https://github.com/loopbackio/strong-soap). I get my data in the service and I can see it in console.log() but I can't send this data to my controller to serve it. I have tried using pipe(map()) and I have looked into this following topics (https://www.freecodecamp.org/news/an-express-service-for-parallel-soap-invocation-in-under-25-lines-of-code-b7eac725702e/) but no luck. I either get 'can't subscribe to undefined' or my request is passing without my controller getting the data and serving it.

Here is my controller.ts

 export class MyController {

    constructor(private readonly service: Service){}

    @Get('/example')
    getAll(@Query() query: Query){
        return this.service.getAll(query);
    }

}

and here is my service

    export class Service {

    private URL = process.env.URL;
    constructor() { }

    async getAll(query: OrderRequest) {
        const request = {
            req: {
               req: query,
            }
        };

        return await soap.createClient(this.URL, (err, client) => {
            if (err) {
                console.error(err);
            } else {
                let method = client.myMethodName;
                method(request, (err, result, envelope, soapHeader) => {
                    
            // here I am getting the data but 'return result. is not sending it to the controller        

console.log(result[0])
return result
                });
            }
        });
    }
}

As I said I have tried with map and pipe like this:

 return await soap.createClient(this.URL).pipe(map((err, client) => {
            if (err) {
                console.error(err);
            } else {
                let method = client.myMethodName; // here I have an error
                method(request, (err, result, envelope, soapHeader) => {
                    console.log(result)
                });
            }
        })) 

but I can't pass my method in the client. Thanks

Upvotes: 1

Views: 1579

Answers (1)

LaCodeM
LaCodeM

Reputation: 829

OK I have found the solution. Thing is the function above was returning the promise so solution was to initialize new promise and handle reject and resolve. Like this we get the results in resolve and take them with .then() in our controller.

Here is the code:

service.ts

getAll(query: Query) {
        const request = {query: {query: query}};
        return new Promise ((resolve, reject) => { // here we add new Promise
            soap.createClient(this.URL, (err, client) => {
                if (err) {
                    return reject(err); // if error reject promise
                } else {
                    return resolve(client); // else resolve and send client
                    });
                }
            });
        }   
    )}

controller.ts

getAll(@Query() query: query){
    console.log(this.service.getAll(query))
    return this.service.getAll(query).then(result=>result)
}

Upvotes: 1

Related Questions