0

I am currently trying to return a value from a function that contains an async function and I'm stuck.

I am currently working with SharePoint 2013 JSOM to extract the email address from a Person column. I have found a nice function that does this and I elected to pass the userID into the function.

The function itself contains console.log and outputs the expected result, however, I need those results up where I called the function from in the first place so I found that I need to use a callback. I cannot extract a variable from the calling method.

    var t = oListItem.get_item('ElementContactFullName').get_lookupId();
    var q = getEmail(t, function(returnedValue){});
    function getEmail(userId, callback) {

    var x = [];
    var context = new SP.ClientContext();
    var web = context.get_web();
    var user = web.get_siteUsers().getById(userId);
    context.load(user);

    context.executeQueryAsync(function() {
        //console.log(user.get_email());
        var y = user.get_email();
        x.push(y);

    }


    , function() {
        console.log("error");

    });

    callback(x);
    }

What I want is q to equal the email address so I can use it elsewhere in the calling function.

What I get is "Undefined" no matter what I try.

I can place console.log in function(returnedValue){}) but that still doesn't let me get at the variable. Not done enough JScript to understand the very complex dicussions on the proposed duplicate.

Mr safe
  • 1
  • 1
  • Possible duplicate of [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) – jonrsharpe May 09 '19 at 14:53
  • Put the callback call where you had put the working log statement? – Bergi May 09 '19 at 16:45
  • …and then yes, you need to put your `console.log` in the callback to log the `returnedValue`. The `q` return value will always be `undefined`. – Bergi May 09 '19 at 17:51

1 Answers1

0
function getEUEmail(userId, JT,  flag, callback) {

    var x = [];

    var contextEU = new SP.ClientContext();

    var web = contextEU.get_web();

    var user = web.get_siteUsers().getById(userId);

    contextEU.load(user);

    contextEU.executeQueryAsync(function() {

        //console.log(user.get_email());

        var y =   "<div class='tablewrapper'>" +
      "<div class='table'>" +
        "<div class='row'>" +
          "<div class='rowspanned cell'>" +
             ' <img class="contacts" src="' + _spPageContextInfo.webServerRelativeUrl + '/_layouts/15/userphoto.aspx?size=M&accountname='+ user.get_email()  +'"/>' +
          "</div>" +
          "<div class='cell'>" +
           user.get_title() +
          "</div>" +
        "</div>" +
        "<div class='row'>" +
          "<div class='empty cell'></div>" +
          "<div class='cell'>" +
            JT +  
          "<div class='cell'>" +
         ' <img class="flag" src="' + window.location.protocol + "//" + window.location.host   + '/SiteAssets/Images/'+ flag  +'.png"/>' +
         //http://staging.intranet.ent.sys.element.com/SiteAssets/Images/EU.png
          "</div>" +
        "</div>" +
        "</div>" +
      "</div>" +
    "</div>"

        x.push(y);

        callback(x);
}

Then to use it

       getEUEmail(t,u, v, function(returnedValueEU) {

            //console.log(returnedValue[0])

            $("#divListItemsEU").append(

            "<style type='text/css'> .tablewrapper { position: relative;  box-sizing: border-box; height:72px} .table {display: table; } .row { display: table-row; padding: 1px; } .cell { display: table-cell; border: none solid red; padding: 3px;} .cell.empty { border: none; width: 75px; } .cell.rowspanned {  position: absolute; top: 0;  bottom: 0; width: 75px; display: block; margin: 1px;} .contacts{ width: 72px; height: 72px;} .flag { width: 30px; height: 20px; }</style> " +

                    "" + returnedValueEU[0]  + 




                    '<br />');
});

    $("#divListItemsEU").html(listEUItemInfoEU);

}

I ended up passing all the values into the getEmail function, building the HTML then worked out how to leverage the callback with some help. Probably not correct, efficient or elegant but it works. Thanks

Mr safe
  • 1
  • 1