Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

355
Visualizações
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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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)
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda