gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:00:25 27 4
gpt4 key购买 nike

用例

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

问题

由于转换问题,更新不会发生。
异常(exception):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;
}

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

更新

完整的异常消息:
 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)

参与者模型

public class Participant
{
public string playerName { get; set; }
public int experience { get; set; }
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; }
}

用于在 Firestore 上创建模型的参与者 typescript 界面
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
}

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

最佳答案

这里有两个解决方案:

  • 你需要用
  • 来装饰这个类
    [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; }
    }
    或者
    通过将参与者转换为 ExpandoObject ExpandoObject Reference
    建议在不使用 Newtonsoft.Json 的情况下转换对象:
    How do you convert any C# object to an ExpandoObject?
    但是使用 Newtonsoft.Json 很容易理解,这就是我所做的:
    var serializedParticipant = JsonConvert.SerializeObject(participant);
    var deserializedParticipant = JsonConvert.DeserializeObject<ExpandoObject>(serializedParticipant);

    //setting the document
    await documentReference.UpdateAsync(deserializedParticipant);
    然后与您的参与者一起更新 Firestore 作为 ExpandoObject而不是 Model.Participant

    关于c# - 从 .Net Core 控制台应用程序更新 Firebase Firestore 中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59794765/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com