Value assignment issue

LabKey Support Forum (Inactive)
Value assignment issue feifei bao  2015-04-29 07:13
Status: Closed
 
Hi, I have a tricky problem when I apply the LABKEY.Query.selectRows() function.

Here is the simple example of my coding:

<script type="text/javascript">
    var requirementId;

    LABKEY.Query.selectRows({
            schemaName: 'lists',
            queryName: 'Main List',
        success: onSuccess,
        failure: onFailure
        });
    
    function onSuccess(data)
    {
        requirementId = data.rows[data.rowCount-1].Name;
    }
    
    function onFailure(errorInfo, options, responseObj)
    {
        if (errorInfo && errorInfo.exception)
            alert("Failure: " + errorInfo.exception);
        else
            alert("Failure: " + responseObj.statusText);
    }
    alert(requirementId);
</script>


I test the value "requirementId" inside function onSuccess(data). It has the true value. But if I test outside the function, it is undefined. How can I use this value in other functions after it has been assigned?

Thank you very much.

Feifei
 
 
bront responded:  2015-04-29 09:42
Feifei,

I am not entirely sure, but I think you have a scope issue.

You could place the alert() within the success function:

 function onSuccess(data)
    {
        requirementId = data.rows[data.rowCount-1].Name;
        alert(requirementId);
    }

Or you could pass the requirementId to an external function:

var requirementId;
    
LABKEY.Query.selectRows({
        schemaName: 'lists',
        queryName: 'Main List',
    success: onSuccess,
    failure: onFailure
    });
    
function onSuccess(data)
    {
      requirementId = data.rows[data.rowCount-1].Name;
      tester(requirementId);
    }
    
function onFailure(errorInfo, options, responseObj)
    {
    if (errorInfo && errorInfo.exception)
      alert("Failure: " + errorInfo.exception);
    else
      alert("Failure: " + responseObj.statusText);
    }
    
alert(requirementId); // undefined

// new function
function tester(reqId) {
    console.log('requirementId' + reqId); // defined
}


I hope this might be of some help.

cheers,

bront
 
Ben Bimber responded:  2015-04-29 09:46
I think there's also an issue of the asynchonous call. see the console output from this:


LABKEY.Query.selectRows({
            schemaName: 'lists',
            queryName: 'Main List',
        success: onSuccess
        });
     
    function onSuccess(data)
    {
        console.log('i will be called second, because the call to the server is asynchronous');
        requirementId = data.rows[data.rowCount-1].Name;
    }
     
    console.log('i will be called first, before the call to the server returns.');
    alert(requirementId);


Bront is also right that you'd have a scope problem too. His suggestion about a new function that is call is probably a good one. however, if onSuccess ends up with only 2 lines (setting the variable and then calling your new function), you might consider just putting your code there.