angular - 可以使用 Angular 将 Firebase 文档及其所有子集合复制到另一个文档

标签 angular firebase google-cloud-firestore

enter image description here

大家好, 正如我们从图片中看到的示例,我想复制集合('planaprendizaje')中包含的所有文档(及其数据)。

例如,从文档 1 中获取数据并放入文档 10。选择我是否需要 planaprendizaje 或其他子集合。

我正在使用 Angular 连接到 Firebase 以尝试获取数据。

最佳答案

这是我的解决方案,但使用可调用的云函数。您可以使用源参数和目标参数。

const functions = require( 'firebase-functions' );
const admin = require( 'firebase-admin' );

const copyDoc = async ( source, dest ) => {
  const docRef = admin.firestore().doc( source );

  const docData = await docRef
    .get()
    .then( ( doc ) => doc.exists && doc.data() )
    .catch( ( error ) => {
      console.error( 'Error reading document', `${source}`, error.message );
      throw new functions.https.HttpsError( 'not-found', 'Copying document was not read' );
    } );

  if ( docData ) {
    await admin
      .firestore()
      .doc( dest )
      .set( docData )
      .catch( ( error ) => {
        console.error( 'Error creating document', `${dest}`, error.message );
        throw new functions.https.HttpsError(
          'data-loss',
          'Data was not copied properly to the target collection, please try again.',
        );
      } );
  }

  const subcollections = await docRef.listCollections();
  for await ( const subcollectionRef of subcollections ) {
    const subPath = `${source}/${subcollectionRef.id}`
    console.log( 'parsing: ', subPath )
    try {
      const snapshot = await subcollectionRef.get()
      const docs = snapshot.docs;
      for await ( const doc of docs ) {
        console.log( `coping: ${subPath}/${doc.id} -> ${dest}/${subcollectionRef.id}/${doc.id}` )
        await copyDoc( `${subPath}/${doc.id}`, `${dest}/${subcollectionRef.id}/${doc.id}` );
      }
    } catch ( e ) {
      throw new functions.https.HttpsError(
        'data-loss',
        `${e.message} -> ${subPath}, please try again.`,
      )
    }
  }
}

exports.set = functions
  .region( 'europe-west3' )
  .https.onCall( async ( {source_doc, destination_doc}, context ) => {
    if ( !context.auth ) {
      // Throwing an HttpsError so that the client gets the error details.
      throw new functions.https.HttpsError( 'unauthenticated', 'The function must be called while authenticated.' );
    }
    try {
      await copyDoc( source_doc, destination_doc )
      return { result: destination_doc  }
    } catch ( e ) {
      throw new functions.https.HttpsError( 'aborted', e.message );
    }
  } );

关于angular - 可以使用 Angular 将 Firebase 文档及其所有子集合复制到另一个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66771381/

相关文章:

javascript - Firebase Firestore 快照读取计数

javascript - Firebase:doc.data() 返回空对象

jquery - 如何在我的 Angular 应用程序中替换 jquery?

javascript - "No ' 访问控制允许来源 ' header is present on the requested resource.The response had HTTP status code 403."

swift - Firebase Firestore 不更新电子邮件验证状态

javascript - 重定向后如何将用户带到完全相同的位置?

java - 合并 Firestore 查询时出现重复数据

html - 这种处理移动/桌面布局的方式是不好的做法吗?

在 RxJs 订阅后,来自服务的 Angular 2 返回数据不可用

firebase 更复杂的验证