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!

2
  • context.executeQueryAsync() is missing Commented Aug 5, 2016 at 8:45
  • where should i Place this method? Commented Aug 5, 2016 at 8:45

1 Answer 1

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.

2
  • Thanks Atish for always helping me it is working perfect now. But I want to know why this function executeQueryAsync() should be here? Commented Aug 5, 2016 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. Commented Aug 5, 2016 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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