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

Angular+Firestore删除功能报错:无效集合引用问题求助

Angular+Firestore删除操作错误排查与解决

执行Firestore删除操作时触发以下错误:

core.mjs:10592 ERROR FirebaseError: Invalid collection reference. Collection references must have an odd number of segments, but Notes/r5GucE9DETw36sJKCMgi has 2.


相关代码

HTML代码

<div class="col-md-4" *ngFor="let note of notesData">
    <div class="card card-body">
        <h5 class="card-title">{{ note.title }}</h5>
        <p class="text-muted">{{ note.description }}</p>
        <div class="d-flex align-items-center">
            <span class="mx-2">
                <i class="bi bi-pencil-square" data-bs-toggle="modal" 
                data-bs-target="#editNotesModal"></i>
            </span>
            <span class="mx-2">
                <i class="bi bi-trash" (click)="deleteNote(note)"></i>
            </span>
        </div>
    </div>
</div>

组件TS代码

noteForm!: FormGroup;
notesData: any;
noteObj: Note = {
  id: '',
  title: '',
  description: ''
}

constructor(private fb: FormBuilder, private noteService: NoteService){
  this.noteForm = this.fb.group({
    title: ['', Validators.required],
    description: ['', Validators.required]
  })
}

ngOnInit(){
  this.fetchAllNotes()
}

addNote(){
  const { value } = this.noteForm
  console.log(value);

  this.noteObj.id = '',
  this.noteObj.title = value.title,
  this.noteObj.description = value.description

  this.noteService.addNote(this.noteObj).then((note)=>{
    if(note){
      alert("Note Added Successfully")
    }
  })
  this.noteForm.reset()
}

fetchAllNotes(){
  this.noteService.getNotes().subscribe((res: Note[])=>{
    console.log(res)
    this.notesData = res;
  })
}

deleteNote(note: Note){
  let delConfirm = confirm('Delete current selected Note?');
  if (delConfirm == true){
    this.noteService.deleteNote(note);
  }
}

Service代码

addNote (note: Note){
  note.id = doc(collection(this.fs, 'id')).id
  return addDoc(collection(this.fs, 'Notes'), note)
}

getNotes():Observable<Note[]>{
  let notesRef = collection(this.fs, 'Notes')
  return collectionData(notesRef, {idField: 'id'}) as Observable<Note[]>
}

deleteNote(note: Note){
  let docRef = doc(collection(this.fs,`Notes/${note.id}`));
  return deleteDoc(docRef)
}

updateNote(note: Note, notes: any){
  let docRef = doc(this.fs, `Notes/${note.id}`);
  return updateDoc(docRef, notes)
}

数据库结构截图

Firebase数据库截图


错误原因与解决方法

错误核心是删除方法中错误地用集合引用构建文档路径:
在deleteNote方法里,collection(this.fs,Notes/${note.id})试图把单个文档路径(Notes/文档ID,2段路径)当作集合引用,但Firestore要求集合引用必须是奇数段路径(比如Notes是1段,Notes/文档ID/子集合是3段),因此触发错误。

修改Service中的deleteNote方法,直接用doc()方法指向目标文档即可,无需嵌套collection():

deleteNote(note: Note){
  let docRef = doc(this.fs, `Notes/${note.id}`);
  return deleteDoc(docRef)
}

另外补充一个优化点:addNote方法中生成文档ID的代码有误,当前doc(collection(this.fs, 'id')).id是基于名为id的集合生成ID,应该改为基于Notes集合生成,确保ID与集合关联:

addNote (note: Note){
  // 基于Notes集合生成文档ID
  const noteDocRef = doc(collection(this.fs, 'Notes'));
  note.id = noteDocRef.id;
  return setDoc(noteDocRef, note);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 01:00:31