You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FirebaseFirestore更新文档时触发cloud_firestore/not-found异常求助

排查Firebase Firestore更新文档时的[cloud_firestore/not-found]异常

执行以下代码尝试更新Firestore中orders集合内的指定文档时,抛出FirebaseException,错误提示为[cloud_firestore/not-found] 请求的文档不存在:

onConfirm: (date)async{
  await FirebaseFirestore.instance.collection('orders')
    .doc(order['orderid']).update(
     {
       'deliverystatus':'shipping',
       'deliverydate':date,
    }, 
  );
}

Firebase控制台截图:
Firebase截图

异常原因分析

  • 文档ID不匹配:order['orderid']获取到的ID与Firestore中实际存在的文档ID不一致,比如存在大小写差异、空格或特殊字符偏差,导致找不到对应文档。
  • 集合名称错误:代码中指定的集合orders与Firebase控制台中的集合名称不匹配(Firestore集合名称区分大小写)。
  • 文档实际不存在:order['orderid']对应的文档并未在orders集合中创建,而update()方法仅能修改已存在的文档,因此触发报错。

解决办法

  • 校验文档ID:在调用update()前添加调试代码,打印order['orderid']的值,与Firebase控制台中的文档ID对比,确保完全一致:
    print('待更新文档ID: ${order['orderid']}');
    
  • 核对集合名称:确认Firebase控制台中的集合名称为orders,与代码中collection('orders')的拼写、大小写完全匹配。
  • 改用set()方法(可选):如果需要在文档不存在时自动创建、存在时更新内容,可将update()替换为带merge: true参数的set():
    await FirebaseFirestore.instance.collection('orders')
      .doc(order['orderid']).set(
       {
         'deliverystatus':'shipping',
         'deliverydate':date,
       },
       SetOptions(merge: true)
    );
    
  • 提前验证文档存在性:更新前先检查文档是否存在,避免抛出异常:
    onConfirm: (date)async{
      final docRef = FirebaseFirestore.instance.collection('orders').doc(order['orderid']);
      final docSnapshot = await docRef.get();
      if (docSnapshot.exists) {
        await docRef.update({
          'deliverystatus':'shipping',
          'deliverydate':date,
        });
      } else {
        print('指定文档不存在,请检查ID或创建文档');
        // 可添加用户提示等逻辑
      }
    }
    

内容的提问来源于stack exchange,提问作者Haris Tallat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 01:40:17