RavenDb, NullReferenceException on Map/Reduce

1

I'm using the latest version of RavenDb .Net Core client with the latest server distribution.

I am trying to add this index:


this.Map = persons => from person in persons
                      let company = this.LoadDocument(person.ExternalEmployeeData.CompanyId)
                      select new
                      {
                          CompanyName = company != null ? company.Name : string.Empty,
                          CompanyRegisteredName = company != null ? company.RegisteredName : string.Empty,
                          person.FirstName,
                          person.Id,
                          person.LastName,
                          person.NationalInsuranceNumber
                      };

this.Reduce = models => from model in models
                        select new
                        {
                            model.CompanyName,
                            model.CompanyRegisteredName,
                            model.FirstName,
                            model.Id,
                            model.LastName,
                            model.NationalInsuranceNumber
                        };

If I add it without the "Reduce" block, it gets added but when querying it, the "CompanyName" and "CompanyRegisteredName" properties are always null in the output, although I can filter over them correctly. So I thought I needed the "Reduce" function. But adding it gives me the following NullReferenceException:


Raven.Client.Exceptions.Documents.Compilation.IndexCompilationException
  HResult=0x80131500
  Message=IndexCompilationException: Object reference not set to an instance of an object., IndexDefinitionProperty='Reduce', ProblematicText='results.Select(model => new {
    CompanyName = model.CompanyName,
    CompanyRegisteredName = model.CompanyRegisteredName,
    FirstName = model.FirstName,
    Id = model.Id,
    LastName = model.LastName,
    NationalInsuranceNumber = model.NationalInsuranceNumber
})' ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Raven.Server.Documents.Indexes.Static.IndexCompiler.HandleReduce(String reduce, FieldNamesValidator fieldNamesValidator, MethodDetectorRewriter methodsDetector, String[]& groupByFields) in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\Documents\Indexes\Static\IndexCompiler.cs:line 392
   --- End of inner exception stack trace ---
   at Raven.Server.Documents.Indexes.Static.IndexCompiler.HandleReduce(String reduce, FieldNamesValidator fieldNamesValidator, MethodDetectorRewriter methodsDetector, String[]& groupByFields) in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\Documents\Indexes\Static\IndexCompiler.cs:line 404
   at Raven.Server.Documents.Indexes.Static.IndexCompiler.CreateClass(String name, IndexDefinition definition) in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\Documents\Indexes\Static\IndexCompiler.cs:line 267
   at Raven.Server.Documents.Indexes.Static.IndexCompiler.Compile(IndexDefinition definition) in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\Documents\Indexes\Static\IndexCompiler.cs:line 73
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Raven.Server.Documents.Indexes.Static.IndexCompilationCache.GetIndexInstance(IndexDefinition definition) in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\Documents\Indexes\Static\IndexCompilationCache.cs:line 46
   at Raven.Server.Documents.Indexes.IndexStore.d__23.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\Documents\Indexes\IndexStore.cs:line 355
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Handlers.Admin.AdminIndexHandler.d__0.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\Documents\Handlers\Admin\AdminIndexHandler.cs:line 50
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Routing.RequestRouter.d__7.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\Routing\RequestRouter.cs:line 116
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Raven.Server.RavenServerStartup.d__11.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Server\RavenServerStartup.cs:line 161
  Source=Raven.Client
  StackTrace:
   at Raven.Client.Exceptions.ExceptionDispatcher.d__3.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Client\Exceptions\ExceptionDispatcher.cs:line 117
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Client.Http.RequestExecutor.d__90`1.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Client\Http\RequestExecutor.cs:line 1014
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Client.Http.RequestExecutor.d__75`1.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Client\Http\RequestExecutor.cs:line 806
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Raven.Client.Http.RequestExecutor.d__75`1.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Client\Http\RequestExecutor.cs:line 806
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Client.Http.RequestExecutor.d__68`1.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Client\Http\RequestExecutor.cs:line 417
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Client.Documents.Operations.MaintenanceOperationExecutor.d__13`1.MoveNext() in C:\Builds\RavenDB-Stable-4.0\src\Raven.Client\Documents\Operations\MaintenanceOperationExecutor.cs:line 64
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Client.Util.AsyncHelpers.RunSync(Func`1 task) in C:\Builds\RavenDB-Stable-4.0\src\Raven.Client\Util\AsyncHelpers.cs:line 79
   at MyProject.DataAccess.RavenDb.ServiceCollectionExtensions.ConfigureStore(IDocumentStore store) in C:\Sviluppo\steelchihuahua\MyProject.DataAccess.RavenDb\ServiceCollectionExtensions.cs:line 65
   at MyProject.DataAccess.RavenDb.ServiceCollectionExtensions.InitializeDocumentStore(IServiceCollection services, Boolean isDevelopment, Boolean isStaging, Boolean isProduction, String contentRootPath, String certificatePath, String certificatePassword, String databaseName, String databaseUrl) in C:\Sviluppo\steelchihuahua\MyProject.DataAccess.RavenDb\ServiceCollectionExtensions.cs:line 45
   at MyProject.RealTimeApi.Startup.ConfigureServices(IServiceCollection services) in C:\Sviluppo\steelchihuahua\MyProject.RealTimeApi\Startup.cs:line 41

With this kind of exception I'm not sure if I'm doing something wrong or if there's a bug in the library, or maybe both.

Anyone does know about this?

Thanks.

EDIT: Experimenting further I found even more problems. I've been trying to add an extremely simple map/reduce, and I got a new kind of (senseless) error, as I am already returning an anonymous object. Also, if I remove the "groupby" statement (which isn't really needed) I'm back to the NullReferenceException: enter image description here

c#
.net-core
ravendb
asked on Stack Overflow Jun 18, 2018 by Matteo Mosca • edited Jun 18, 2018 by Matteo Mosca

2 Answers

1

Turns out that all I needed was, after the Map definition, was:

this.StoreAllFields(FieldStorage.Yes);

This preserves the data in the index. And then I had to explicitly project my query, even if I was already using the index output as type T of my IQueryable:

var items = await pagedQuery.ProjectInto<TIndexOutput>()

Still, the weird error messages reported by the server should be addressed. A "NullReferenceException" means a scenario that is not meant to be, and that is not covered by RavenDB unit tests. Also the message that says that I need to output an anonymous type when I was already doing so is very misleading.

answered on Stack Overflow Jun 19, 2018 by Matteo Mosca
0

There is no need for the Map-Reduce index.

Map-Reduce is for perfoming data aggregations.

Just check on the company.Name (instead of just on company) as follows:

this.Map = persons => from person in persons
    let company = this.LoadDocument<T>(person.ExternalEmployeeData.CompanyId)
    select new
    {
      // check on company.Name
      CompanyName = company.Name != null ? company.Name : string.Empty,
      CompanyRegisteredName = company.Name != null ? company.RegisteredName : string.Empty,
      person.FirstName,
      person.Id,
      person.LastName,
      person.NationalInsuranceNumber
    };
answered on Stack Overflow Jun 18, 2018 by Danielle • edited Jun 19, 2018 by Danielle

User contributions licensed under CC BY-SA 3.0