0

I have a couple of functions in Javascript / Jquery where I am passing a property to one function in turn passes down to another function.

The problem that I am getting is that the property in the first function has been populated but the second function the property is underfined.

ExecuteSQL: function (sql, onSuccess) {                
    try {
        if (this.Database != null) {
            this.Database.transaction(function (tx) {
                tx.executeSql(sql, [], function (d, r) {
                    if (this.onSuccess) this.onSuccess(r);
                });
            });
        }
        else {
            alert("Could not execute SQL, the database connection is closed.");
        }

    }
    catch (e) {
        alert("Failed to execute SQL," + e);
    }
},

The onSuccess is a value at the ExecuteSQL scope but empty in the transaction scope.

Any help would be great.

Thanks Paul.

0

1 Answer 1

2

In this case you can just do:

function (d, r) { if (onSuccess) onSuccess(r); }

Just to make the check explicitly for function you can do:

function (d, r) { if (typeof onSuccess === 'function') onSuccess(r); }

Since onSuccess is a variable that holds the function reference that is defined in the scope of the function ExecuteSQL you can just access it as is inside the callback and it is not associated as a property of the instance, so this.onSuccess doesn't fetch you anything (Leaving the fact that this is not the this inside the ExecuteSQL function ).

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thank you, I did try that before but could get it to work either but this was because of another JS problem. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.