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

开发博客编辑功能调用Firebase update()提示成功但文档未更新

问题根因

你代码的问题出在react-hook-form的表单字段注册逻辑错误,Firestore更新时自动跳过了undefined字段,所以无报错但数据无变更:

  • 所有表单输入组件都注册到了同一个名为test的字段下,提交表单时updatePost方法接收的content、published参数均为undefined
  • Firestore的ref.update()方法会自动忽略值为undefined的字段,不会触发报错,因此你会收到更新成功的提示,但数据库没有写入任何数据
修复方案

修改Postform组件中两个输入控件的register参数,匹配你要提交的字段名即可:

// textarea组件修改register参数
<textarea
  className='form-control'
  name="content"
  {...register("content", { required: true })}
></textarea>

// checkbox组件修改register参数
<input
  className="form-check-input"
  name="published"
  type="checkbox"
  {...register("published", { required: true })}
/>
调试建议

你可以在updatePost方法中添加日志打印,确认提交的参数是否符合预期,方便快速定位类似问题:

const updatePost = async ({ content, published }) => {
  console.log('提交的参数:', {content, published})
  await postRef.update({
    content,
    published,
  });

  reset({ content, published });
  toast.success("文章更新成功!");
};

内容的提问来源于stack exchange,提问作者arsh coding

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 18:15:06