从 .Net Core 控制台应用程序更新 Firebase Firestore 中的文档

rex*_*ror 1 c# firebase .net-core google-cloud-firestore

用例

使用 Firestore 作为持久性形式的 Angular Firebase 应用程序需要与 Discord Bot 通信。我已经构建了一个同步机器人来在现有的外部机器人和 Web 应用程序之间进行调解。有足够的信息来查找和更新文档。

问题

由于转换问题,更新不会发生。
例外:Unable to create converter for type Models.Participant

在尝试了几种解决方案后,主要是使用 json 转换,我简化了代码,以便更好地了解情况。我假设缺少一些明显的东西,但由于我对 firebase (firestore) 缺乏经验,我现在无法看到什么。

public async Task<bool> NextTurn(string encounterName)
{
    var encounterSnapshotQuery = await _encountersCollection.WhereEqualTo("name", encounterName).GetSnapshotAsync();

    foreach (DocumentSnapshot encounterSnapshot in encounterSnapshotQuery.Documents)
    {
        Dictionary<string, object> data = encounterSnapshot.ToDictionary();
        var name = data["name"].ToString();

        if (name == encounterName)
        {
            var participants = data["participants"].ToParticipants();
            var orderedParticipants = participants.OrderByDescending(x => x.initiative + x.roll).ToList();

            var current = orderedParticipants.Single(x => x.isCurrent != null && x.isCurrent is bool && (bool)x.isCurrent);
            var currentIndex = orderedParticipants.FindIndex(x => x.characterName == current.characterName);
            var next = orderedParticipants[currentIndex + 1];

            current.hasPlayedThisTurn = true;
            current.isCurrent = false;
            next.isCurrent = true;

            var updates = new Dictionary<FieldPath, object>
            {
                { new FieldPath("participants"),  orderedParticipants }
            };

            try
            {
                await encounterSnapshot.Reference.UpdateAsync(updates);
            }
            catch (Exception ex)
            {
                _logger.LogError(new EventId(), ex, "Update failed.");
            }
        }

    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)

如果方法中有明显错误,也欢迎提出建议。

更新

完整的异常消息:

 at Google.Cloud.Firestore.Converters.ConverterCache.CreateConverter(Type targetType)
   at Google.Cloud.Firestore.Converters.ConverterCache.<>c.<GetConverter>b__1_0(Type t)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Google.Cloud.Firestore.Converters.ConverterCache.GetConverter(Type targetType)
   at Google.Cloud.Firestore.SerializationContext.GetConverter(Type targetType)
   at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value)
   at Google.Cloud.Firestore.Converters.ListConverterBase.Serialize(SerializationContext context, Object value)
   at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value)
   at Google.Cloud.Firestore.WriteBatch.<>c__DisplayClass12_0.<Update>b__1(KeyValuePair`2 pair)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Google.Cloud.Firestore.WriteBatch.Update(DocumentReference documentReference, IDictionary`2 updates, Precondition precondition)
   at Google.Cloud.Firestore.DocumentReference.UpdateAsync(IDictionary`2 updates, Precondition precondition, CancellationToken cancellationToken)
Run Code Online (Sandbox Code Playgroud)

参与者模型

 at Google.Cloud.Firestore.Converters.ConverterCache.CreateConverter(Type targetType)
   at Google.Cloud.Firestore.Converters.ConverterCache.<>c.<GetConverter>b__1_0(Type t)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Google.Cloud.Firestore.Converters.ConverterCache.GetConverter(Type targetType)
   at Google.Cloud.Firestore.SerializationContext.GetConverter(Type targetType)
   at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value)
   at Google.Cloud.Firestore.Converters.ListConverterBase.Serialize(SerializationContext context, Object value)
   at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value)
   at Google.Cloud.Firestore.WriteBatch.<>c__DisplayClass12_0.<Update>b__1(KeyValuePair`2 pair)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Google.Cloud.Firestore.WriteBatch.Update(DocumentReference documentReference, IDictionary`2 updates, Precondition precondition)
   at Google.Cloud.Firestore.DocumentReference.UpdateAsync(IDictionary`2 updates, Precondition precondition, CancellationToken cancellationToken)
Run Code Online (Sandbox Code Playgroud)

用于在 Firestore 上创建模型的参与者打字稿界面

export interface Participant {
    playerName: string,
    characterName: string,
    initiative: number,
    roll: number,
    playerUid: string,
    joined: Date,
    portraitUrl: string,
    level: number,
    experience: number,
    isCurrent: boolean,
    sizeModifier: number,
    type: string,
    abilities: {
        strength: number,
        dexterity: number,
        constitution: number,
        intelligence: number,
        wisdom: number,
        charisma: number
    },
    hasPlayedThisTurn: boolean
}
Run Code Online (Sandbox Code Playgroud)

请注意,我已经尝试更改 C# 模型来尝试解决此问题。这是目前的状态。不管我做了什么改变,消息都是一样的。

Phi*_*p S 7

这里有两个解决方案:

  1. 你需要用
[FirestoreData]
public class Participant
{
    [FirestoreProperty]
    public string playerName { get; set; }

    [FirestoreProperty("playerExperience")] //you can give the properties custom names as well
    public int experience { get; set; }
 
    //so on
    public int level { get; set; }
    public string characterName { get; set; }
    public string playerUid { get; set; }
    public object joined { get; set; }
    public string type { get; set; }
    public object abilities { get; set; }
    public int roll { get; set; }
    public bool? isCurrent { get; set; }
    public int sizeModifier { get; set; }
    public int initiative { get; set; }
    public bool? hasPlayedThisTurn { get; set; }
    public string portraitUrl { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

或者

通过将参与者转换为 ExpandoObject ExpandoObject 参考

建议在不使用 Newtonsoft.Json 的情况下转换对象: 如何将任何 C# 对象转换为 ExpandoObject?

但是使用 Newtonsoft.Json 很容易理解,这就是我所做的:

var serializedParticipant = JsonConvert.SerializeObject(participant);
var deserializedParticipant = JsonConvert.DeserializeObject<ExpandoObject>(serializedParticipant);

//setting the document
await documentReference.UpdateAsync(deserializedParticipant);
Run Code Online (Sandbox Code Playgroud)

然后与您的参与者一起更新 firestore 作为ExpandoObject代替Model.Participant

  • 我很高兴它成功了!顺便说一句,我相信如果您使用 ExpandoObject 方式,我认为您不需要任何 firestore 属性,除非您在其他地方正常转换它。我确实建议仅使用 Firestore 属性,但在某些情况下它不起作用。 (3认同)