dhodges47 / SCORM-LearningManagementSystem

Open Source SCORM Learning Management System demo
MIT License
152 stars 76 forks source link

Outermoust LMS - LMSGetValue return problem #4

Closed rscott78 closed 4 years ago

rscott78 commented 5 years ago

Hi, first off, thanks for providing this!

I have been trying to trouble-shoot a problem where the LMSGetValue method always returns undefined. I believe I've narrowed the problem down to the OutermostLMSV5.js file.

In this function, the ajax call requests the value from the API, then in the success function, it returns the result. However, if I'm not mistaken, the ajax call runs asynchronously, so the return never happens from the scope of the function itself, which is why the calling function always gets a value of undefined for that call.

I suspect this is probably something that has been recently implemented in Chrome (i.e, they force async calls on the main thread).

function _LMSGetValue(name) {
        ...
        $.ajax({
            type: "POST",
            url: "/api/LMSGetValue",
            data: lmsInfo,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            async: false,
            success: function (response) {        
                lmsInfo = response;
                API.LastError = lmsInfo.errorCode;
                API.LastErrorString = lmsInfo.errorString;
                // check error code from server
                if (lmsInfo.errorCode != "0") {
                    return "false";
                }
                else {
                    // Since this is called async, the return goes no-where
                    return lmsInfo.returnValue;
                }
            },
            error: function (request, error) {
                // Ajax call failed
                API.LastError = "101"; //general exception
                API.LastErrorString = error;
                API.LastErrorDiagnostic = "AJAX error";
                WriteToDebug("Ajax error");
                WriteToDebug(error.Message);
                return "false";
            }
        });
dhodges47 commented 5 years ago

No, if you'll notice in the $.ajax call, there is a parameter "async:false", which forces the function to wait for a response.

This part of the code is pretty old, and I would take pains to run it asynchronously if I were starting over!

Did you get into the debugger for your browser to see if an error is happening?

You might have to debug the server code to see what the call to the database is returning.

On Mon, Nov 25, 2019 at 6:45 PM Ryan Scott notifications@github.com wrote:

Hi, first off, thanks for providing this!

I have been trying to trouble-shoot a problem where the LMSGetValue method always returns undefined. I believe I've narrowed the problem down to the OutermostLMSV5.js file.

In this function, the ajax call requests the value from the API, then in the success function, it returns the result. However, if I'm not mistaken, the ajax call runs asynchronously, so the return never happens from the scope of the function itself, which is why the calling function always gets a value of undefined for that call.

Am I missing something?

function _LMSGetValue(name) { ... $.ajax({ type: "POST", url: "/api/LMSGetValue", data: lmsInfo, dataType: "json", contentType: "application/json; charset=utf-8", async: false, success: function (response) { lmsInfo = response; API.LastError = lmsInfo.errorCode; API.LastErrorString = lmsInfo.errorString; // check error code from server if (lmsInfo.errorCode != "0") { return "false"; } else { // Since this is called async, the return goes no-where return lmsInfo.returnValue; } }, error: function (request, error) { // Ajax call failed API.LastError = "101"; //general exception API.LastErrorString = error; API.LastErrorDiagnostic = "AJAX error"; WriteToDebug("Ajax error"); WriteToDebug(error.Message); return "false"; } });

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dhodges47/SCORM-LearningManagementSystem/issues/4?email_source=notifications&email_token=AAB5OFU3ZXRYGNUW5T7BYYDQVRPTBA5CNFSM4JRQJPM2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4H36ZFSQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB5OFUNJH2GXMSZCPWDKEDQVRPTBANCNFSM4JRQJPMQ .

rscott78 commented 5 years ago

You are correct, it's definitely not an async issue.

I believe the problem is with the success function itself. The return lmsInfo.returnValue; is returning from the success function, but not the parent _LMSGetValue function. In essense, function _LMSGetValue has no return in it, which is why all calls to it come back as undefined.

Here's how I patched it up, and this works for me:

        var returnValue = '';

        $.ajax({
            type: "POST",
            url: "/api/LMSGetValue",
            data: lmsInfo,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            async: false,
            success: function (response) {
                console.log("getValuResponse", response);
                lmsInfo = response;
                API.LastError = lmsInfo.errorCode;
                API.LastErrorString = lmsInfo.errorString;
                // check error code from server
                if (lmsInfo.errorCode != "0") {
                    return "false";
                }
                else {
                    console.log("_LMSGetValue return", lmsInfo.returnValue, name);
                    returnValue = lmsInfo.returnValue;
                    return lmsInfo.returnValue;

                }
            },
            error: function (request, error) {
                // Ajax call failed
                API.LastError = "101"; //general exception
                API.LastErrorString = error;
                API.LastErrorDiagnostic = "AJAX error";
                WriteToDebug("Ajax error");
                WriteToDebug(error.Message);
                return "false";

            }
        });

        return returnValue;
dhodges47 commented 5 years ago

Wow, interesting. The strange thing is, a live version of this code has been working on a commercial site for years and has serviced tens of thousands of course sessions. If I get a chance I'll do some debugging of my own to see what changed. In the meantime, do you want to commit your change to the main branch? Or should I?

On Tue, Nov 26, 2019 at 9:36 AM Ryan Scott notifications@github.com wrote:

You are correct, it's definitely not an async issue.

I believe the problem is with the success function itself. The return lmsInfo.returnValue; is returning from the success function, but not the parent _LMSGetValue function. In essense, function _LMSGetValue has no return in it, which is why all calls to it come back as undefined.

Here's how I patched it up, and this works for me:

    var returnValue = '';

    $.ajax({
        type: "POST",
        url: "/api/LMSGetValue",
        data: lmsInfo,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        async: false,
        success: function (response) {
            console.log("getValuResponse", response);
            lmsInfo = response;
            API.LastError = lmsInfo.errorCode;
            API.LastErrorString = lmsInfo.errorString;
            // check error code from server
            if (lmsInfo.errorCode != "0") {
                return "false";
            }
            else {
                console.log("_LMSGetValue return", lmsInfo.returnValue, name);
                returnValue = lmsInfo.returnValue;
                return lmsInfo.returnValue;

            }
        },
        error: function (request, error) {
            // Ajax call failed
            API.LastError = "101"; //general exception
            API.LastErrorString = error;
            API.LastErrorDiagnostic = "AJAX error";
            WriteToDebug("Ajax error");
            WriteToDebug(error.Message);
            return "false";

        }
    });

    return returnValue;

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dhodges47/SCORM-LearningManagementSystem/issues/4?email_source=notifications&email_token=AAB5OFV7UQA5IHVPUVEEXOTQVUX5LA5CNFSM4JRQJPM2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFGHACA#issuecomment-558657544, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB5OFVATL5H2PP4QA7NRMTQVUX5LANCNFSM4JRQJPMQ .

dhodges47 commented 4 years ago

This was fixed.