繁体   English   中英

将 xlsx 或 csv 文件上传到 firebase

[英]uploading xlsx or csv files to firebase

您好,我正在尝试使用我从前端调用的云函数将文件( .csv、5000 行和 6 列的 .xlsx)上传到 firebase,云函数启动并在运行几秒钟后出错“代码":"4" "详细信息":"超过截止日期"

(火焰计划)

  1. 上传很慢
  2. 使用代码 4 获得截止日期超出错误
  3. 我收到错误后它使服务器崩溃

下面代码的解释:
我做了一个 for of 循环,它遍历一个看起来像这样的数组数组 [['Dufour', 'Annie', 'annie.duf@gmail.com', 33683333005, 'f'], ['john',' Doe', 'john@gmail.com', 33223424, 'm']] 然后我格式化每一行以便将其构造为具有许多字段的 Firestore 文档并尝试将其添加到 Firestore 然后它的崩溃我不知道什么时候它的崩溃真的很随机​​,当我尝试一个较小的文件(1000行)时,它在执行几秒钟后也崩溃了

  export const createNewLeads = functions.region("europe-west3").https.onCall(async (data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError('failed-precondition', 'The function must be called' + 'while authenticated.');
  }
  const { category, subcategory, allLeads, downloadUrl, file_id, fileDetails } = data;
  
  for (const lead of allLeads) {
    
    const leads_infos = format_lead_infos(lead , fileDetails);
    const leads_meta = format_lead_metadata(downloadUrl, file_id, fileDetails);
    const db_lead_ref = admin.firestore().collection('leads');
    const new_lead_doc_id =  db_lead_ref.doc().id;

     db_lead_ref
    .doc(new_lead_doc_id).set({
      lead_id: new_lead_doc_id,
      category: category,
      subcategory: subcategory,
      createdOn: Date(),
      uploaded_by: context.auth.uid,
      purchased_by: null,
      metadata: leads_meta,
      infos: leads_infos

    }).then(() => {
      functions.logger.info('createLeads SUCCESS', { docId: new_lead_doc_id });
      return {
        user_id: new_lead_doc_id,
        created: true,
      };
    }).catch((error: any) => {
      functions.logger.info('createLeads ERROR', { error });
      return {
        error: true,
        err_mess: error,
      };
    });
  }  
});

据我所知,您没有正确处理写入 Firestore 的异步性质,因为您没有awaitset()的调用。 您还将返回循环中第一个成功的文档写入的结果,这似乎很可疑。

这应该更接近您的需要:

try {
  for (const lead of allLeads) {    
    const leads_infos = format_lead_infos(lead , fileDetails);
    const leads_meta = format_lead_metadata(downloadUrl, file_id, fileDetails);
    const db_lead_ref = admin.firestore().collection('leads');
    const new_lead_doc_id =  db_lead_ref.doc().id;

    await db_lead_ref.doc(new_lead_doc_id).set({
      lead_id: new_lead_doc_id,
      category: category,
      subcategory: subcategory,
      createdOn: Date(),
      uploaded_by: context.auth.uid,
      purchased_by: null,
      metadata: leads_meta,
      infos: leads_infos
    });
  }  
}
catch (error: any) => {
  functions.logger.info('createLeads ERROR', { error });
  return {
    error: true,
    err_mess: error,
  };
}
functions.logger.info('createLeads SUCCESS', { docId: new_lead_doc_id 
return {
  user_id: new_lead_doc_id,
  created: true,
};

在这段代码中,我们await每个文档写入操作,然后如果其中任何一个失败,则返回一个错误,并且只有在所有这些操作都成功时才成功。

您好,弗兰克,感谢您帮助我们,我在运行几秒钟后复制了您提供给我们的代码,我从 firebase 控制台日志中收到了代码的此错误错误

export const createNewLeads = functions.region("europe-west3").https.onCall(async (data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError('failed-precondition', 'The function must be called' + 'while authentificated.');
  }
  const { category, subcategory, allLeads, downloadUrl, file_id, fileDetails } = data;
  const db_lead_ref = admin.firestore().collection('leads');
  const new_lead_doc_id =  db_lead_ref.doc().id;
  
  try {
    for (const lead of allLeads) {    
      const leads_infos = format_lead_infos(lead , fileDetails);
      const leads_meta = format_lead_metadata(downloadUrl, file_id, fileDetails);
  
      await db_lead_ref.doc(new_lead_doc_id).set({
        lead_id: new_lead_doc_id,
        category: category,
        subcategory: subcategory,
        createdOn: Date(),
        uploaded_by: context.auth.uid,
        purchased_by: null,
        metadata: leads_meta,
        infos: leads_infos
      });
    }  
  } catch (error:any) {
    functions.logger.info('createLeads ERROR', { error });
  return {
    error: true,
    err_mess: error,
  }
  
  }
  functions.logger.info('createLeads SUCCESS', { docId: new_lead_doc_id })
return {
  user_id: new_lead_doc_id,
  created: true,
}
});

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM