-2

I'm using a soap file to perform simple math accounts, but when I'm going to use the result in my controller to print in JSON, my function returns a "Promise {}" right down here I will leave my function and my controller

const CalcSum = require('../sum');
const calcSum = new CalcSum();
class CalculatorController{
        adder (req,res){
        const{
            value1,
            value2,
        }= req.body;

        const result = calcSum.sum(value1,value2);
        console.log(result);
        return res.status(200).json({
          Response: result
        })
      }
    }
module.exports = CalculatorController;
 //end class CalculatorController; 
 // consolo.log(result) returns Promise {<pending>} in the terminal


//the first class is my controller, next is my function 

const soap = require("soap");
const wsdl = "http://www.dneonline.com/calculator.asmx?wsdl";

class CalcSum{
  async sum(value1, value2) {
    const soapClient = await soap.createClientAsync(wsdl);
    
      await soapClient.Add({ intA: value1, intB: value2 }, (err, result) => {
        then(function(result) {
          console.log(result); 
          return result.AddResult;
        })
        .then(function(result) {
          console.log(result); 
        });
      })
    }
}

module.exports = CalcSum;

//end class/function CalcSum
Lagoss
  • 1
  • 1
  • 2
    Have you considered waiting for the promise to resolve? – Kevin B May 13 '21 at 18:59
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Heretic Monkey May 13 '21 at 18:59
  • 2
    Yes, it's an async function. [Those return a promise](https://stackoverflow.com/questions/38884522/why-is-my-asynchronous-function-returning-promise-pending-instead-of-a-val). – VLAZ May 13 '21 at 19:00
  • 2
    I'm surprised you're getting that much, considering you're not returning the result of `await soapClient.Add` – Heretic Monkey May 13 '21 at 19:00
  • remove the 2 then's and do `const {AddResult} = await soapClient.Add(...` – Lawrence Cherone May 13 '21 at 19:00
  • yes I know it returns a promise, but I can't get my CalcSum class to return the result to my CalculatorController. I tried to use .then, but was unsuccessful – Lagoss May 13 '21 at 19:05
  • 3
    `(err, result) => { then(function(result) {` looks wrong – Bergi May 13 '21 at 19:11
  • I managed to solve my problem, I ended up changing my strategy, I will put my solution below in case someone has gone through the same problem or something similar, thanks to everyone who helped – Lagoss May 13 '21 at 23:08

1 Answers1

0
const CalcSum = require('../sum');
const calcSum = new CalcSum();
class CalculatorController{
       adder (req, res){
        const result = calcSum.sum(req,res);
      }
    }
module.exports = CalculatorController;
//this is my controller, now i'm passing the request and the answer to the method

const soap = require("soap");
const wsdl = "http://www.dneonline.com/calculator.asmx?wsdl";

class CalcSum {
  async sum(req, res) {
    const {
      value1,
      value2
    } = req.body;
    const soapClient = await soap.createClientAsync(wsdl);
    
      await soapClient.Add({ intA: value1, intB: value2 }, (err, result) => {
      if (err) {
        console.log(err);
      }
      return res.status(200).json({
        Response: result.AddResult 
      })
    })
  }
}
module.exports = CalcSum;

//this is my sum class, i am sending the sum result in a JSON
Lagoss
  • 1
  • 1