我正在尝试在有角度的应用程序中实现Promise
操作。我希望能够单击一个按钮并删除一个Firestore文档,但是我的代码无法正常工作。因此,我正在使用所有Firestore操作的服务,并在组件中调用该服务
station.service.ts
it('selects element if passed in route', async () => {
const $route = {params: {id: '256'}};
const wrapper = shallowMount(AbcModel, {
mocks: {$route},
store, localVue
});
expect(someOtherFunction).toHaveBeenCalled();
// Ideally await the Promise directly...
// but if that isn't possible then calling await Promise.resolve()
// queues the rest of the test at the back of PromiseJobs
// allowing any pending callbacks to run first
await Promise.resolve();
expect(selectElement).toHaveBeenCalled(); // SUCCESS
});
station.component.ts
DELETE
我在console.log消息中找不到ID,看起来像这样
import { Injectable } from "@angular/core";
import { AngularFirestore } from "@angular/fire/firestore";
import { Station } from "../../models/station.model";
@Injectable({
providedIn: "root"
})
export class StationService {
constructor(private afs: AngularFirestore) {}
deleteStation(stationId: string) {
this.afs.doc("stations/" + stationId).delete();
}
}
如何修复我的代码?提醒,这是我第一次使用Firestore。
答案 0 :(得分:0)
最终找到了该错误。在station.component.ts
ngOnInit()方法中,将id设置为对象而不是id。像这样将其设置为id
ngOnInit() {
this.stationService.getStations().subscribe(data => {
this.stations = data.map(e => {
return {
id: e.payload.doc.id,
...e.payload.doc.data()
} as Station;
});
});
}
现在,将返回一个字符串类型的实际ID和每个对象,然后可以将它们传递到delete(id: string)
方法中。