未捕获的TypeError:无法读取属性' toString'未定义的JavaScript

时间:2016-08-05 08:36:39

标签: javascript asp.net sharepoint

我的JavaScript代码中有以下功能,当我运行它时,显示以下错误Cannot read property 'toString' of undefined我正在开发一个SharePoint Web应用程序。

function startTest(quizID) {

   currentQuizItem = quizList.getItemById(quizID);
   var quizName;
   context.load(currentQuizItem);
   var userName = user.get_title();

   var audiencenames = currentQuizItem.get_fieldValues()["Audienca"];
   var straudience = audiencenames.toString();
   straudience = straudience.toUpperCase();
   straudience = straudience.replace(/\s/g, '');

   userName = userName.toUpperCase();
   userName = userName.replace(/\s/g, '');
   var rezultati = straudience.match(userName);
   alert(rezultati);
}

请帮助我!

1 个答案:

答案 0 :(得分:0)

context.executeQueryAsync()是我的代码遗失的内容。我添加了它并且它有效。

function startTest(quizID) {

    currentQuizItem = quizList.getItemById(quizID);
    var quizName;
    context.load(currentQuizItem);
    var userName = user.get_title();
    context.executeQueryAsync(function() {
            var audiencenames = currentQuizItem.get_fieldValues()["Audienca"];
            var straudience = audiencenames.toString();
            straudience = straudience.toUpperCase();
            straudience = straudience.replace(/\s/g, '');

            userName = userName.toUpperCase();
            userName = userName.replace(/\s/g, '');
            var rezultati = straudience.match(userName);
            alert(rezultati);
        },

        function() {
            // handle error
        });
}