【问题标题】:Setting a document in the Google Firestore database在 Google Firestore 数据库中设置文档
【发布时间】:2020-04-19 04:54:04
【问题描述】:

我在 Visual Studio 的 Google Firestore 数据库中设置文档时似乎遇到了问题。我正在遵循 Google 在其网站 (https://firebase.google.com/docs/firestore/manage-data/add-data) 上提供的指南。我不是 C# 方面的专家,我不知道这是什么意思。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Cloud.Firestore;

namespace ConsoleApp1
{
class Program
{
    static void Main(string[] args)
    {
        FirestoreDb db = FirestoreDb.Create("dzapp1-a2bfd");
        Console.WriteLine("Created Cloud Firestore client with project ID: {0}", "dzapp1-a2bfd");
        Console.ReadKey();
        DocumentReference docRef = db.Collection("cities").Document("LA");
        Dictionary<string, object> city = new Dictionary<string, object>
        {
            { "name", "Los Angeles" },
            { "state", "CA" },
            { "country", "USA" }
        };
        WriteResult writeResult = await docRef.SetAsync(city);
        Console.WriteLine(writeResult.UpdateTime);
    }
}

}

程序似乎对这一行有问题:

WriteResult writeResult = await docRef.SetAsync(city);

它说 'await' 运算符只能在异步方法中使用。 正如我所说,我不是 C# 专家,我很无助,有没有人知道如何重写代码以使一切正常?我尝试用谷歌搜索异步方法并将代码放入其中,但我该从那里去哪里?如何调用该方法? (如果那是你用它做的)。谢谢大家的回答,真的很感谢。

【问题讨论】:

    标签: c# firebase async-await google-cloud-firestore


    【解决方案1】:

    您可能已经阅读过,您应该在标有 async 修饰符的方法中使用 await。 仔细查看该示例的完整源代码将阐明 await 如何在异步方法中使用:

    https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/ffd0e648acbcfe26500f9247e54c10b15f10079e/firestore/api/AddData/Program.cs#L45-L53

    具体来说:查看AddDocAsMap的签名以及它是如何从Main调用的

    【讨论】:

    • 非常感谢你,我真的很感激。我很惭愧我还没有找到这个,我不得不打扰你,非常感谢你。我很高兴你能在这里帮助像我这样的新手 :) 再次感谢你。
    • 不用担心,很高兴您发现这很有用。我们每个人都是很多学科的新手;)
    猜你喜欢
    • 2018-11-18
    • 2019-07-22
    • 1970-01-01
    • 2021-12-04
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    相关资源
    最近更新 更多