0

I need the value of the variable 'result' to make a comparison. Im having problem to get this 'result' value.

Does anyone know how to fix it?

**Im using node to make an REST application, and I need this to result in my GET method

async function get(req, res, next) {
  var result = 0;
  try {
    const context = {};
    const login = {};
    login.user = req.params.user, 100;
    login.pass = req.params.pass, 200;

    var optionsgetmsg = {
      host: 'URL', // here only the domain name
      // (no http/https !)

      path: '/controller/verifica.asp?a=' + login.user + '&b=' + login.pass, // the rest of the url with parameters if needed
      method: 'GET' // do GET
    };


    var reqGet =  https.request(optionsgetmsg,  function (res) {

       res.on('data',  function (d) {
        jsonResponse = JSON.parse(d);
        result = jsonResponse.message;
      });

    });
    reqGet.end();
    reqGet.on('error', function (e) {
      console.error(e);
    });
    context.id = parseInt(req.params.id, 10);
    console.log(result);
    //problem
    if(result == "yes"){
    const rows = await operadores.find(context);

    if (req.params.id) {
      if (rows.length === 1) {
        res.status(200).json(rows[0]);
      } else {
        res.status(404).end();
      }
    } else {
      res.status(200).json(rows);
    }
  }
}
5
  • if(result = 'yes') do you really want to assign in this conditional or is this a typo? Commented Nov 1, 2019 at 18:53
  • Its a String, I've updated the code Commented Nov 1, 2019 at 19:01
  • What do you think this login.user = req.params.user, 100; will do ? Commented Nov 1, 2019 at 19:02
  • The 'user' parameter in the URL Commented Nov 1, 2019 at 19:03
  • The , 100 is confusing because it will not do anything. Commented Nov 1, 2019 at 19:07

1 Answer 1

1

You can use a library that does HTTP requests and supports promises, something like axios or refactor your current https.request and convert it to a promise, here is an example:

function makeRequest({ user, password }) {
  return new Promise((resolve, reject) => {
    const options = {
      host: 'URL', 
      path: `/controller/verifica.asp?a=${encodeURIComponent(user)}&b=${encodeURIComponent(pass)}`
      method: 'GET'
    };
    const reqGet = https.request(options, function (res) {
       res.on('data', function (d) {
          result = JSON.parse(d);
          resolve(result.message);
      });

    });
    reqGet.on('error', function (e) {
      reject(e);
    });
    reqGet.end();
  });
}

Then you can simply do this:

const result = await makeRequest(req.params);

I've used some concepts that you may not be familiar with, template literal and object destructuring

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.