Search code examples
javascriptparse-platformparse-cloud-code

Javascript TypeError: Cannot call method 'slice' of undefined


I'm currently learning javascript for a simple Parse CloudCode script I'd like to write. Unfortunately I'm getting the error posted below which seems to be quite common, unfortunately my lack of skills in this area aren't allowing me to fix the issue even with the resources available.

Any solutions, tutorials or explanations are very appreciated.

Error (as logged by the iOS app):

error calling 'updateLastSeen' function: Error Domain=Parse Code=141 "TypeError: Cannot call method 'slice' of undefined
    at e.query.first.success (main.js:11:58)
    at e.<anonymous> (Parse.js:14:28998)
    at e.i (Parse.js:14:27703)
    at e.a.value (Parse.js:14:27063)
    at e.i (Parse.js:14:27830)
    at e.a.value (Parse.js:14:27063)
    at e.i (Parse.js:14:27830)
    at e.a.value (Parse.js:14:27063)
    at e.<anonymous> (Parse.js:14:27774)
    at e.i (Parse.js:14:27703)" UserInfo={code=141, temporary=0, error=TypeError: Cannot call method 'slice' of undefined
    at e.query.first.success (main.js:11:58)
    at e.<anonymous> (Parse.js:14:28998)
    at e.i (Parse.js:14:27703)
    at e.a.value (Parse.js:14:27063)
    at e.i (Parse.js:14:27830)
    at e.a.value (Parse.js:14:27063)
    at e.i (Parse.js:14:27830)
    at e.a.value (Parse.js:14:27063)
    at e.<anonymous> (Parse.js:14:27774)
    at e.i (Parse.js:14:27703), NSLocalizedDescription=TypeError: Cannot call method 'slice' of undefined
    at e.query.first.success (main.js:11:58)
    at e.<anonymous> (Parse.js:14:28998)
    at e.i (Parse.js:14:27703)
    at e.a.value (Parse.js:14:27063)
    at e.i (Parse.js:14:27830)
    at e.a.value (Parse.js:14:27063)
    at e.i (Parse.js:14:27830)
    at e.a.value (Parse.js:14:27063)
    at e.<anonymous> (Parse.js:14:27774)
    at e.i (Parse.js:14:27703)}

Main.js CloudCode script (concerned part):

var query = new Parse.Query(Parse.User);
    query.equalTo('email', request.params.email);
    query.first({
        success: function(user) {
            var mutableLastSeenDictionariesArray = user.lastSeen.slice(0);

            for (var i = 0; i < mutableLastSeenDictionariesArray.length; i++) {
                var lastSeen = mutableLastSeenDictionariesArray[i];

                if (lastSeen[response.params.email]) {
                    i = mutableLastSeenDictionariesArray.length+1;
                    mutableLastSeenDictionariesArray.splice(i, 1, request.params["builtDictionary"]);

                    user.lastSeens = mutableLastSeenDictionariesArray;
                    user.save;
                }
            }
        },

        error: function(error) {
            response.error(error.code, "Error: " + error.message);
        }
    });

Thank you.


Solution

  • So the issue was that the .lastSeens property which is an array was empty and therefore undefined (null).

    The solution was to implement a check if (typeof user.lastSeens == 'undefined') {...} and handle accordingly.

    Thanks to @danh for pointing me in the right direction.