【问题标题】:Wait for an Import Operation of Firestore collections to finish等待 Firestore 集合的导入操作完成
【发布时间】:2020-07-23 15:25:12
【问题描述】:

我有以下 https 可调用云功能,可导入备份中找到的所有文档。

    const client = await auth.getClient();

    const path = `${timestamp}`;

    const projectId = await auth.getProjectId();

    // we change the action for importDocuments
    const url = `https://firestore.googleapis.com/v1/projects/${projectId}/databases/(default):importDocuments`;
    const backup_route = `gs://${BUCKET_NAME}/${path}`;
    return client.request({
        url,
        method: 'POST',
        data: {
            inputUriPrefix: backup_route,
        }
    }).then(async (res) => {
        console.log(`Backup restored from folder ${backup_route}`);
        return Promise.resolve(true);
    }).catch(async (e) => {
        return Promise.reject(new functions.https.HttpsError('internal', e.message));
    })

问题是在成功开始导入操作后,promise 解决了。但它不会等到导入完成。

我阅读了以下有关导入和长时间运行操作的文档,但没有找到我要查找的内容。 https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases/importDocuments https://firebase.google.com/docs/firestore/reference/rest/Shared.Types/Operation

【问题讨论】:

    标签: javascript asynchronous google-cloud-firestore google-cloud-functions


    【解决方案1】:

    是的,它就是这样工作的。 importDocuments 的 API 文档说它返回一个 Operation。根据操作文档:

    此资源表示一个长时间运行的操作,它是网络 API 调用的结果。

    我通常阅读 API 的方式是您需要获取此操作对象并使用methods on operations 定期轮询它。看起来get 会为你做这件事。您必须从调用importDocuments 获得的有效负载中将操作的名称传递给它。最终,它将为您提供一个 done 属性设置为 true 的操作。

    导入的时间很可能比你的函数运行的时间长,所以你的函数的调用者不能简单地等到导入完成。客户端将需要轮询另一个不断检查操作是否完成的函数。

    【讨论】:

    • 谢谢,您的回复很有帮助。
    • 如果您有时间,习惯上会使用右侧的复选框将有用的答案标记为正确,这样提供答案的人就可以获得荣誉。
    猜你喜欢
    • 2020-07-02
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多