Garmaine Staff asked 2 years ago

I have the following https callable cloud function that imports all documents found in a backup.

    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));
    })

The problem is that the promise resolves after successfully beginning the import operation. But it does not wait until the import is finished.

I read the following documentation on the import and long-running operations but didn't find what I was looking for. https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases/importDocuments https://firebase.google.com/docs/firestore/reference/rest/Shared.Types/Operation