-1

When I run my code, it gives me an error "ReferenceError: MyVariable is not defined" on var comm_Id, I am trying to fetch the page ID and then generate the URL to call the rest API

this "${community.id}" fetches the ID of the page dynamically.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var comm_Id = ${community.id} //MyVariable being the supposed ID of the page
    var cat_id = ${coreNode.ancestors[0].id}; //Category ID of the boards parent
    var restUrl = "/"+comm_Id+"/restapi/vc/categories/id/"+cat_id;

    $.ajax({
        type: "GET",
        url: restUrl,
        contentType: "text/xml; charset=utf-8",
        data: "DATA",
        success: function (response) {

            var index = 0;
            var fltBoardID = "";
            var boardId="";

            var categoryNode = $(response).find("category > title");
            var categoryTitle = $(categoryNode).text();
            $("#category-title").html("<p>More in the "+categoryTitle+" category</p>");

            $(response).find("board > id").each(function () {
                boardId = $(this).text();

                var boardTitle = $(response).find("board > title:eq("+index+")");
                var otherBoardsList = $(boardTitle).text();
                $("#Other-Boards-List").append("<ul><li><a href='"+"/t5/board/bd-p/"+boardId+"'>"+otherBoardsList+"</a></li></ul>");
                index++;
            });
        },
        error: function (response) {
            $('#Other-Boards-List').html('Failed to load the content');
        }
    });
});
</script>

<div id="Other-Boards-List">
   <div id="category-title"></div>
   <p style="font-size:18px;">Browse other boards</p>    
</div>
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Varun Luthra
  • 23
  • 3
  • 11

1 Answers1

1

Assuming ${community.id} is replaced by a sequence of characters before the script runs, you have to the value so that it is interpreted as a string.

var comm_Id = "${community.id}";
//should result in:
var comm_Id = "MyVariable";

Without quotes, the interpreter would look for a variable called MyVariable.

As noted by @Michael, you should also quote cat_id (though not necessary if it is an integer value without leading zeroes).

p.s. Single quotes can be used instead of double quotes as well - the only difference being the escaping of the string delimiter character (ref).

Community
  • 1
  • 1
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • 2
    Good call. Same problem exists for `cat_id`, assuming that it is supposed to be a string. If it's a number it would be OK as is. But since `cat_id` is only used where it's concatenated with another string, may as well quote it and play it safe. – Michael Geary Oct 03 '13 at 07:04