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="" /> }
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.
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)
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; }
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!)
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"/> } }
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?
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.
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.
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)
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)
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?
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"
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
}
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.
Awesome - always the little things that catch us out!
is working on a reply...