2sic / 2sxc

DNN + 2sxc = #DNNCMS - This tool helps web designers and developers prepare great looking content in DNN (DotNetNuke). It's like mixing DNN with Umbraco and Drupal :)
http://2sxc.org
MIT License
145 stars 40 forks source link

[Bug] GetQuery() failing on Query with Pages DataSource #3350

Closed jeremy-farrance closed 5 months ago

jeremy-farrance commented 6 months ago

I'm submitting a

[x] bug report

...about

[x] Razor templating, C#

Expected Behavior

In previous versions you could use App.GetQuery("Query Name") wrapped in AsItems() to easily loop through pages using the Pages DataSource.

Current Behavior in 2sxc v17.06.03

Using code very similar to the tutorial example Data and Query - MyData, App.GetQuery() and AsItems(), expand Get data from a Query using App.GetQuery( ... ) - I am getting the following error. This happens with or without params and also whether or not I add .GetStream("Default"):

Error: System.ArgumentException: Conversion with AsItems failed, required=true. Got null. Parameter name: list 
at ToSic.Sxc.Data.Internal.CodeDataFactory.<>c__DisplayClass61_0.<AsItemList>g__FallbackOrErrorAndLog|0(String fallbackMsg, 
String exMsg) in C:\Projects\2sxc\2sxc\Src\Sxc\ToSic.Sxc\Data\Internal\Factory\CodeDataFactory_TypedItem.cs:line 87 
at ToSic.Sxc.Data.Internal.CodeDataFactory.AsItemList(Object list, Boolean required, IEnumerable`1 fallback, 
Int32 recursions, Boolean propsRequired) 
in C:\Projects\2sxc\2sxc\Src\Sxc\ToSic.Sxc\Data\Internal\Factory\CodeDataFactory_TypedItem.cs:line 97 
at ...

The failing code is:

@inherits Custom.Hybrid.RazorTyped
@* @using ToSic.Razor.Blade *@

@{
  var query = App.GetQuery("MegaMenu2");
}

<div @Kit.Toolbar.Default(MyItem)>
  <h3>Pages</h3>
  @foreach(var currPage in AsItems(query))
  {
    <p>@currPage.Id, @currPage.Title - @currPage.Bool("HasChildren")</p>
  }
</div>

The error when we add .GetStream():

Error: System.NullReferenceException: Object reference not set to an instance of an object. 
at ASP._Page_Portals_0_2sxc_PDPR_Pages__Nav__MegaMenus_cshtml.Execute() 
in C:\HostingSpaces\pdpr\pdpr2023.accuraty.us\www\Portals\0\2sxc\PDPR\Pages__Nav--MegaMenus.cshtml🎯:line 10 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at

The only code change for the slightly different error:

  @foreach(var currPage in AsItems(query.GetStream("Default")))

The Visual Query works fine and returns the expected number of pages, 184: image image

Instructions to Reproduce the Problem

Make the simplest query you can using the Pages DataSource returning all values to the Default stream. Try my code above or use the Tutorial linked above as reference.

Your environment

jeremy-farrance commented 6 months ago

Sorry if I left out the obvious. Above code works fine with other DataSources. My guess is that it has something to do with having children in the result?

I thought I could test that by using a Remove Attribute/Property Modifier, but even without the Children and Parent fields in the JSON results, AsItems() still fails, same errors.

jeremy-farrance commented 5 months ago

Okay, I got this working. I don't believe there was a bug. I just had to adjust how I was using AsItems(). The final working code:

  <h4>Version 1: From Query.GetStream Default</h4>
  <pre>
  @foreach (var page in AsItems(query.GetStream("Default")))
  {
    <span>@page.Id - @page.String("Name"), ParentId=@page.Int("ParentId")</span>
  }
  </pre>
iJungleboy commented 5 months ago

awesome, thanks.