Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Amir Khan 1282 posts 2739 karma points
    Jun 05, 2013 @ 00:13
    Amir Khan
    0

    Razor Script Index was outside the bounds of the array

    So I have this script that loops through all of my nodes and filters by document type. I'm using to generate some XML for a map. When I get the "Take" value above a certain number, it gives me this "Razor Script Index was outside the bounds of the array" error.

    Any Ideas?

    @inherits umbraco.MacroEngines.DynamicNodeContext
    <?xml version="1.0" encoding="utf-8"?>
    <markers>
    @{
    // Get root node:
    var root = Library.NodeById(-1);

    // Get all descendants, filter by type:
    var nodes = root.Descendants().Where("NodeTypeAlias == @0 || NodeTypeAlias == @1", "LocationPageMC", "LocationPageSN");

    // Loop through the filtered nodes, displaying the properties:
    foreach (var node in nodes.Take(200))
    {
    var map = @node.googleMap;
    var lat = map.Split(',')[0];
    var lng = map.Split(',')[1];
    var zoom = map.Split(',')[2];
    <marker name="@node.Name" lat="@lat" lng="@lng" type="hcr" address="@node.addressLine1" address2="@node.addressLine2" city="@node.city" state="@node.state" postal="@node.zip" phone="@node.phone" web="@node.Url" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" />
    }

    }
    </markers>
  • Ian Black 54 posts 230 karma points
    Jun 05, 2013 @ 10:48
    Ian Black
    0

    Hi Amir,

    Have you tried turning on the debug trace by appending ?umbDebugShowTrace=true onto the end of your URL?
    Also make sure you have this in your web.config: <add key="umbracoDebugMode" value="true" />

    It might be that one of your map variables doesn't have 3 comma seperated values and so it's tripping up when your trying to split the string and and return one of the array values.

  • Amir Khan 1282 posts 2739 karma points
    Jun 05, 2013 @ 16:16
    Amir Khan
    0

    Hi Ian, error is below, should I just check for that value you think? I don't see anything in the error that actually says WHERE in the nodes the error is occurring.

     

    Error Loading Razor Script (file: / Location Finder/ List All Locations) Index was outside the bounds of the array.    at CallSite.Target(Closure , CallSite , Object , Int32 )
      at ASP._Page_macroScripts_LocationFinder_ListAllLocations_cshtml.Execute() in c:\inetpub\wwwroot\macroScripts\LocationFinder\ListAllLocations.cshtml:line 16
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
      at System.Web.WebPages.WebPage.ExecutePageHierarchy()
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
      at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage)
      at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage)
    0.367773632 0.366252
    macro Error loading MacroEngine script (file: /LocationFinder/ListAllLocations.cshtml, Type: ''
    Index was outside the bounds of the array.
      at CallSite.Target(Closure , CallSite , Object , Int32 )
      at ASP._Page_macroScripts_LocationFinder_ListAllLocations_cshtml.Execute() in c:\inetpub\wwwroot\macroScripts\LocationFinder\ListAllLocations.cshtml:line 16
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
      at System.Web.WebPages.WebPage.ExecutePageHierarchy()
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
      at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage)
      at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage)
      at umbraco.macro.loadMacroScript(MacroModel macro)
      at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)
    0.36795336 0.000180
     

     

  • Ian Black 54 posts 230 karma points
    Jun 05, 2013 @ 16:19
    Ian Black
    0

    I would definitely do a check to see if that array index exists before tying to get it's value as at least we can then rule that out.

    There seems to be a mention in the error of "ListAllLocations.cshtml:line 16". What code is on that line?

  • Amir Khan 1282 posts 2739 karma points
    Jun 05, 2013 @ 16:23
    Amir Khan
    0

    Line 16 is this: var lng = map.Split(',')[1];

    And when I try and check like this, I get a "the name lat does not exist in the current context"

    @inherits umbraco.MacroEngines.DynamicNodeContext
    <?xml version="1.0" encoding="utf-8"?>
    <markers>
    @{
    // Get root node:
    var root = Library.NodeById(-1);

    // Get all descendants, filter by type:
    var nodes = root.Descendants().Where("NodeTypeAlias == @0 || NodeTypeAlias == @1", "LocationPageMC", "LocationPageSN");

    // Loop through the filtered nodes, displaying the properties:
    foreach (var node in nodes)
    {
    if(@node.HasProperty("googleMap") && @node.GetProperty("googleMap").Value != String.Empty)
    {
    var map = @node.googleMap;
    var lat = map.Split(',')[0];
    var lng = map.Split(',')[1];
    var zoom = map.Split(',')[2];
    }
    else {
    var lat = 0;
    var lng = 0;
    var zoom = 0;
    }

    <marker name="@node.Name" lat="@lat" lng="@lng" type="hcr" address="@node.addressLine1" address2="@node.addressLine2" city="@node.city" state="@node.state" postal="@node.zip" phone="@node.phone" web="@node.Url" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" />
    }

    }
    </markers>
  • Ian Black 54 posts 230 karma points
    Jun 05, 2013 @ 16:58
    Ian Black
    1

    So I think it's erroring because one of your googleMap propetyies doesn't have a longitude value. We just need to put in a bit of checking first. Something like this:
    (Sorry I still haven't gotten to grips with formatting the code well in these forums!) 

    var lat = 0;
    var lng = 0;
    var zoom = 0;

    if (@node.HasProperty("googleMap") && @node.GetProperty("googleMap").Value != String.Empty) {
    string[] map = @node.googleMap.Split(',');

    if (map.Length > 0) { lat = map[0]; }
    if (map.Length > 1) { lng = map[1]; }
    if (map.Length > 2) { zoom = map[2]; }

    // Put your code here
    }
  • Amir Khan 1282 posts 2739 karma points
    Jun 05, 2013 @ 18:44
    Amir Khan
    0

    So I ended up going with this to prefent it from ever plotting a facility that wasn't complete. You were absolutely right, there was just a few random facilities that had incorrect lat or lon and that's what was throwing the error.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    <?xml version="1.0" encoding="utf-8"?>
    <markers>
    @{
    // Get root node:
    var root = Library.NodeById(-1);

    // Get all descendants, filter by type:
    var nodes = root.Descendants().Where("NodeTypeAlias == @0 || NodeTypeAlias == @1", "LocationPageMC", "LocationPageSN");

    // Loop through the filtered nodes, displaying the properties:
    foreach (var node in nodes)
    {
    if(node.HasProperty("googleMap") && node.GetProperty("googleMap").Value != String.Empty){
    var map = @node.googleMap;
    var lat = map.Split(',')[0];
    var lng = map.Split(',')[1];
    var zoom = map.Split(',')[2];
    <marker name="@node.Name" lat="@lat" lng="@lng" address="@node.addressLine1" address2="@node.addressLine2" city="@node.city" state="@node.state" postal="@node.zip" phone="@node.phone" web="@node.Url" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" category="@node.NodeTypeAlias"/>
    }
    }

    }
    </markers>

  • Ian Black 54 posts 230 karma points
    Jun 06, 2013 @ 00:02
    Ian Black
    0

    Awesome - always the little things that catch us out!

Please Sign in or register to post replies

Write your reply to:

Draft