0

I have the following function in my JavaScript code and when I run it shows me the following error Cannot read property 'toString' of undefined I am developing an SharePoing web application.

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

please help me!

1

context.executeQueryAsync() is missing in your code. Add it and try.

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

Also make sure that your variables are not undefined before using them. Ex. You can set condition before using them.

  • Thanks Atish for always helping me it is working perfect now. But I want to know why this function executeQueryAsync() should be here? – Nderon Hyseni Aug 5 '16 at 9:02
  • after loading anything like context.load(currentQuizItem);, you need this function to call that everything has been loaded successfully. To learn more about it please google. I can not add more here. – Atish Dipongkor - MVP Aug 5 '16 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.