首页 > 解决方案 > RavenDb,Map/Reduce 上的 NullReferenceException

问题描述

我正在使用最新版本的 RavenDb .Net Core 客户端和最新的服务器分发。

我正在尝试添加此索引:


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
                        };

如果我在没有“Reduce”块的情况下添加它,它会被添加,但是在查询它时,“CompanyName”和“CompanyRegisteredName”属性在输出中始终为空,尽管我可以正确过滤它们。所以我认为我需要“减少”功能。但是添加它会给我以下 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

对于这种异常,我不确定我是否做错了什么,或者库中是否存在错误,或者两者兼而有之。

有人知道吗?

谢谢。

编辑:进一步试验我发现了更多问题。我一直在尝试添加一个非常简单的 map/reduce,但我遇到了一种新的(无意义的)错误,因为我已经返回了一个匿名对象。此外,如果我删除“groupby”语句(这并不是真正需要的),我会回到 NullReferenceException: 在此处输入图像描述

标签: c#.net-coreravendb

解决方案


事实证明,在 Map 定义之后,我所需要的只是:

this.StoreAllFields(FieldStorage.Yes);

这会保留索引中的数据。然后我必须显式地投影我的查询,即使我已经使用索引输出作为我的 IQueryable 的类型 T:

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

尽管如此,应该解决服务器报告的奇怪错误消息。“NullReferenceException”表示不应该存在的场景,并且 RavenDB 单元测试未涵盖该场景。此外,当我已经这样做时,说我需要输出匿名类型的消息也非常具有误导性。


推荐阅读