-1

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.

donkey
  • 1,343
  • 13
  • 32
  • 4
    The error says that `user.lastSeen` is `undefined`. We need to see the code leading up to that line rather than the code after it. – James Thorpe Mar 14 '16 at 16:13
  • @JamesThorpe Updated the code. Thanks. – donkey Mar 14 '16 at 17:46
  • Does the object User always have the property lastSeen? Is it spelled correctly? It seems that the property lastSeen is undefined. – matanso Mar 14 '16 at 17:58
  • @JamesThorpe it can be Null. – donkey Mar 14 '16 at 18:13
  • 1
    @ge0rges, it can be null, and it definitely is null in the case of the crash. Fix it by adding defensive code of the null case... `if (user.lastSeen) { var mutable... } else { // user.lastSeen is null }` – danh Mar 14 '16 at 19:17
  • @danh nope it still occurs after the check is in place, same crash log. – donkey Mar 16 '16 at 16:34
  • The error is definitely that slice is being invoked on null either on the line posted or elsewhere or both. The only alternative is to find it and guard against it. As debug step, start commenting out dangerous looking stuff until it goes away. Once it does, the last thing you took out is the culprit. – danh Mar 16 '16 at 16:55

1 Answers1

0

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.

donkey
  • 1,343
  • 13
  • 32