In my Xamarin Forms app I’m creating a database and 2 documents using Couchbase lite. After creating these 2 documents and putting properties into them from the server, there’s no problem in reading that data by using:
var database = Manager.SharedInstance.GetDatabase("indigodb");
var retrievedDocument = database.GetExistingDocument("user");
List<string> companiesList = new List<string>();
IDictionary<string, object> contents = retrievedDocument.Properties;
foreach (KeyValuePair<string, object> entry in contents)
{
if (entry.Key == "Companies" && entry.Value != null)
foreach (string item in (entry.Value as JArray).ToObject<string[]>())
companiesList.Add(item);
}
entry.Value
is stored as a JArray when putting the properties at earlier stage.
The problem arises when it comes to updating the document using:
var userDocument = database.GetExistingDocument("user");
SavedRevision newVersion = userDocument.Update(rev =>
{
var propertiesUser = rev.UserProperties;
propertiesUser["Code"] = Globals.User.ApplicantCode;
propertiesUser["FirstName"] = Globals.User.FirstName;
propertiesUser["LastName"] = Globals.User.LastName;
propertiesUser["Companies"] = Globals.User.Companies;
rev.SetUserProperties(propertiesUser);
return true;
});
where Globals.User.ApplicantCode
, FirstName
and LastName
are strings and Globals.User.Companies
is of type List.
When I’m trying to go through the properties data again using the first code above, entry.Value
now becomes of type generic list and not as JArray anymore so that throws an exception.
I’m trying to see what I’m doing wrong in reading the document properties data or in updating the document (or both), as I cannot seem to find any help online. I hope you understand the issue well.
Thanks!