0

I'm writing a little program which does a GET request and returns the result, which work. Homewer, I cannot manage how to get the value of userShortEmail (it always returns undefined when I call the getUserInfo(session) function (It's supposed to return a String. I tried with timeout and callbacks but no luck so far.

Can you help me? Thanks

function callWebsite(session, args, next) {
        var userShortEmail = "";
        
        userShortEmail = getUserInfo(session);
        
        
        var x = session.message.text.split(" ").pop();
        x = jiraIssue.substring(0, jiraIssue.length - 1);
        session.sendTyping();
        setTimeout(function () {

            var options = putHeader('/bikes/yellow' + x, "GET", userShortEmail);

            function callback(error, response, body) {
                var info = JSON.parse(body);
                if (!error && response.statusCode == 200) {
                    session.endDialog(issueDetails(info));
                }
                else {
                    session.endDialog(info.errorMessages);
                }
            }
            request(options, callback);
                    
        }, 300);

    }

Edit : Here's the sode of getUserInfo function :

function getUserInfo(session) {
    setTimeout(function () {
    var conversationId = session.message.address.conversation.id;
    

    connector.fetchMembers(session.message.address.serviceUrl, conversationId, function (err, result) {
        if (err) {
            console.log('There is some error');
        }
        else {
            result.forEach(function (result) {
                if (session.message.user.id === result.id) {
                    console.log("User is " + result.userPrincipalName);
                    return result.userPrincipalName;
                }
            });
        }
    });
    }, 3000);
}
roud9
  • 51
  • 1
  • 8
  • 1
    Where are the implementation details for getUserInfo(session), also why are you setting a timeout there? – Adam H Aug 02 '18 at 18:45
  • 2
    can you show us getUserInfo() function structure, I debut it is asynchronous logic and need a async/await (jS sugar) so we can use it in your main function with few modification. – Poode Aug 02 '18 at 18:45
  • Hi, I've updated my initial post to include the requested code. Thanks – roud9 Aug 02 '18 at 19:39
  • getUserInfo is always going to return undefined because the body of the function is a call to setTimeout and doesn't return a value. Why are you using setTimeout? – Adam H Aug 02 '18 at 19:44
  • Yup, [that `return result.userPrincipalName;` is not gonna work](https://stackoverflow.com/q/14220321/1048572). (Not just because it is asynchronous, but also because it is in a `forEach` callback). Can you also show us your attempt at using a callback to `getUserInfo`? – Bergi Aug 02 '18 at 19:44

1 Answers1

1

Well, I got slightly bored at work and decided to help you out here. You are calling async functions all over the place and not waiting for responses before continuing. So I went ahead and converted your code to use promises to wait for responses from your async methods before moving on. You could use async/await instead but that's up to you, this is just an example from some random dude on the internet.

function getUserInfo(session) {
  return new Promise(function(resolve, reject) {
    let conversationId = session.message.address.conversation.id;

    connector.fetchMembers(session.message.address.serviceUrl, session.message.address.conversation.id, function(err, result) {
      if (err) {
        reject(err);
      } else {
        let ret;

        result.forEach(function(result) {
          if (session.message.user.id === result.id) {
            // set the return value
            result = result.userPrincipalName;
            // return false to break out of the loop
            return false;
          }
        });

        resolve(ret);
      }
    });

  });
}

function getYellowBikeDetails(userShortEmail, xParam) {
  return new Promise(function(resolve, reject) {
    var options = putHeader('/bikes/yellow' + x, "GET", userShortEmail);

    request(options, function(error, response, body) {
      var info = JSON.parse(body);

      if (!error && response.statusCode == 200) {
        resolve(issueDetails(info));
      } else {
        reject(info.errorMessage);
      }
    });

  });
}

function callWebsite(session, args, next) {

  getUserInfo(session)
    .catch((err) => console.log(err)) // something went wrong here, do your error handling
    .then((userShortEmail) => {
      // you need to check and make sure userShortEmail was found and is not undefined.
      var x = session.message.text.split(" ").pop();
      x = jiraIssue.substring(0, jiraIssue.length - 1);
      session.sendTyping();

      getYellowBikeDetails(userShortEmail, x)
        .catch((err) => console.log(err)) // something went wrong here, do your error handling
        .then((response) => {
          session.endDialog(response);
        });
    });
}
Adam H
  • 1,750
  • 1
  • 9
  • 24
  • Wow I didn't expected that! Thanks, you really helped me and it works now. – roud9 Aug 03 '18 at 16:42
  • @roud9 glad I could help, is there anything you are having trouble understanding with this solution that I can expand on you help you in the future? – Adam H Aug 03 '18 at 20:46