在使用 Angular Fire 和 Firebase 模拟器时,有时会遇到文档更新无法正常工作的问题。原因是在使用模拟器时,需要手动启动 update()
事件,而不是自动触发。
以下是解决方法的代码示例:
import { AngularFirestore } from '@angular/fire/firestore';
import { FirebaseEmulatorService } from './firebase-emulator.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private firestore: AngularFirestore, private emulatorService: FirebaseEmulatorService) {}
updateDocument() {
const docRef = this.firestore.collection("test-collection").doc("test-document");
docRef.update({testField: "new value"})
.then(() => this.emulatorService.triggerUpdateEvent(docRef));
}
}
在上面的示例中,我们使用 AngularFirestore
和 FirebaseEmulatorService
来更新文档。然后我们手动触发 update()
事件,将文档的引用传递给 FirebaseEmulatorService
的 triggerUpdateEvent()
方法。
这样,我们就可以在使用 Angular Fire 和 Firebase 模拟器时正常更新文档了。