"Failed to update index for `equipmentsbytype`: DbError

I was trying to execute query on a view, first time its work perfectly fine and on the second launch I was getting an exception "“Failed to update index for equipmentsbytype: DbError”

Below is the code is was trying to execute

            var view = database.GetView("equipmentsbytype");
            view.SetMap((doc, emit) =>
            {
                // retrieving operation type and its document
                if (doc[Constants.TYPE].Equals(Constants.TYPE_EQUIPMENT) && doc[Constants.OPERATION].Equals(type) && doc[Constants.STATUS].Equals(Constants.ACTIVE))
                {
                    emit(doc[Constants.EQUIPMENT], doc);
                }

            }, Constants.VIEW_VERSION);

            //create query with descending order
            Query query = view.CreateQuery();
            query.Descending = true;

            var equipmentsDetails = query.Run();

and the stack trace is

"Failed to update index for equipmentsbytype: DbError, "

	StackTrace
	at Couchbase.Lite.Storage.SystemSQLite.SqlitePCLRawStorageEngine.RunInTransaction(RunInTransactionDelegate block)\r\n   
	at Couchbase.Lite.Storage.SystemSQLite.SqliteCouchStore.RunInTransaction(RunInTransactionDelegate block)\r\n   
	at Couchbase.Lite.Database.RunInTransaction(RunInTransactionDelegate transactionDelegate)\r\n   
	at Couchbase.Lite.Query.Run()\r\n   
	at DataLite.Models.Equipment.GetEquipmentsByType(Database database, String type) in C:\\Users\\AJAY\\Documents\\windows\\DataLite\\Models\\Equipment.cs:line 72"	string

	
	$exception	{"Failed to update index for `equipmentsbytype`: DbError, "}	Couchbase.Lite.CouchbaseLiteException
  •   CBLStatus	{Status: DbError}	Couchbase.Lite.Status
      Code	DbError	Couchbase.Lite.StatusCode
    
  •   Data	{System.Collections.ListDictionaryInternal}	System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
      HResult	-2146233088	int
      HelpLink	null	string
    
  •   IPForWatsonBuckets	{142813044}	System.UIntPtr
    
  •   InnerException	null	System.Exception
      IsTransient	false	bool
      Message	"Failed to update index for `equipmentsbytype`: DbError, "	string
      RemoteStackTrace	null	string
      Source	"Couchbase.Lite.Storage.SystemSQLite"	string
      StackTrace	"   at Couchbase.Lite.Storage.SystemSQLite.SqlitePCLRawStorageEngine.RunInTransaction(RunInTransactionDelegate block)\r\n   at Couchbase.Lite.Storage.SystemSQLite.SqliteCouchStore.RunInTransaction(RunInTransactionDelegate block)\r\n   at Couchbase.Lite.Database.RunInTransaction(RunInTransactionDelegate transactionDelegate)\r\n   at Couchbase.Lite.Query.Run()\r\n   at DataLite.Models.Equipment.GetEquipmentsByType(Database database, String type) in C:\\Users\\AJAY\\Documents\\lehman-caves-windows\\DataLite\\Models\\Equipment.cs:line 72"	string
    
  •   TargetSite	{Boolean RunInTransaction(Couchbase.Lite.RunInTransactionDelegate)}	System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
      WatsonBuckets	null	object
      _HResult	-2146233088	int
      _className	null	string
    
  •   _data	{System.Collections.ListDictionaryInternal}	System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
      _dynamicMethods	null	object
    
  •   _exceptionMethod	{Boolean RunInTransaction(Couchbase.Lite.RunInTransactionDelegate)}	System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
      _exceptionMethodString	null	string
      _helpURL	null	string
    
  •   _innerException	null	System.Exception
    
  •   _ipForWatsonBuckets	{142813044}	System.UIntPtr
      _message	"Failed to update index for `equipmentsbytype`: DbError, "	string
      _remoteStackIndex	0	int
      _remoteStackTraceString	null	string
    
  •   _safeSerializationManager	{System.Runtime.Serialization.SafeSerializationManager}	System.Runtime.Serialization.SafeSerializationManager
      _source	"Couchbase.Lite.Storage.SystemSQLite"	string
    
  •   _stackTrace	{sbyte[96]}	object {sbyte[]}
      _stackTraceString	null	string
      _watsonBuckets	null	object
      _xcode	-532462766	int
    
  •   _xptrs	{0}	System.IntPtr
    
  •   Static members

@ajaykoppisetty I don’t see where you get type in doc[Constants.OPERATION].Equals(type).

It looks like something is coming up null in your map code. You might want to put in some defensive checks or run with a debugger and look at your values in doc when it crashes.

@hod.greeley most of them are constants in the map code. I have rechecked by debugging step by step and the i was getting the exact error on query.Run() line. There is no change in inputs or constants for first launch and second launch. But I’m facing this error only on second launch.

Your guidance is very much needed in solving this issue.

Isn’t your map function going to throw an NPE if a document doesn’t have a TYPE or OPERATION or STATUS field?

i was getting the exact error on query.Run() line.

View indexing happens (lazily) when the view is queried.

There is no change in inputs or constants for first launch and second launch.

Have any documents been added or modified? That would cause the index to update, calling the map function.

Yikes, I just realized that Hod is right — your map function uses an external variable type. If that’s not a constant, what you’re doing is illegal. A map function has to be a pure function, that always does the same thing given the same inputs. It can’t use external state like that type variable that could change from one call to the next. Please read our docs on views, which explain this in greater detail.

Oh okay, thank you @jens