Javascript TypeError: Cannot call method 'slice' of undefined

Javascript TypeError: Cannot call method 'slice' of undefined

我目前正在学习 javascript 我想编写一个简单的 Parse CloudCode 脚本。不幸的是,我收到下面发布的错误,这似乎很常见,不幸的是,我在这方面缺乏技能,即使有可用资源也无法解决问题。

非常感谢任何解决方案、教程或解释。

错误(由 iOS 应用记录):

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脚本(关注部分):

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);
        }
    });

谢谢。

所以问题是作为数组的 .lastSeens 属性 是空的,因此未定义(null)。

解决方案是实施检查 if (typeof user.lastSeens == 'undefined') {...} 并进行相应处理。

感谢@danh 为我指明了正确的方向。