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 |
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. |