使用 ngxs-lab/firestore-plugin 时如何在我的对象中获取 firebase 文档 ID
How to get the firebase document ID inside my object when using ngxs-lab/firestore-plugin
我使用 NGXS 进行状态管理,还使用 firestore-plugin(https://github.com/ngxs-labs/firestore-plugin)。
如何在我的对象中添加文档的 ID?
我已经创建了我的商店:
import { Injectable } from '@angular/core';
import { NgxsFirestore } from '@ngxs-labs/firestore-plugin';
@Injectable({
providedIn: 'root',
})
export class SpotsFirestore extends NgxsFirestore<Spot> {
protected path = 'seasons/2021/spots';
}
我已经创建了状态和动作:
import { Injectable } from '@angular/core';
import { Emitted, NgxsFirestoreConnect, StreamEmitted } from '@ngxs-labs/firestore-plugin';
import { Action, NgxsOnInit, Selector, State, StateContext } from '@ngxs/store';
import { InitializeSpotsAction } from './spots.actions';
import { SpotsFirestore } from './spots.firestore';
export interface SpotsStateModel {
spots: Spot[];
}
@State<SpotsStateModel>({
name: 'spots',
defaults: {
spots: [],
},
})
@Injectable()
export class SpotsState implements NgxsOnInit {
@Selector()
static spots(state: SpotsStateModel) {
return state.spots;
}
constructor(private spotsFireStore: SpotsFirestore, private ngxsFirestoreConnect: NgxsFirestoreConnect) {}
ngxsOnInit(ctx?: StateContext<any>) {
this.ngxsFirestoreConnect.connect(InitializeSpotsAction, {
to: (action) => this.spotsFireStore.collection$(),
});
}
@Action(StreamEmitted(InitializeSpotsAction))
getAllEmitted(ctx: StateContext<SpotsStateModel>, { action, payload }: Emitted<InitializeSpotsAction, Spot[]>) {
ctx.patchState({ spots: payload });
}
}
我的模型是这样的:
interface Spot {
id: string;
name: string;
description: string;
checkinValue: number;
imageUrl: string;
coordinates: firebase.firestore.GeoPoint;
}
很明显,id 不在我的 firebase 文档中,但正在识别它。不过有了它肯定对我有用
我想我应该重写 NgxsFirestore 中的某些内容,但我不知道如何重写?
是否必须将其包含在文档中?
可以!您只需要在名为 idField
的服务中创建一个 属性,如下所示:
import { Injectable } from '@angular/core';
import { NgxsFirestore } from '@ngxs-labs/firestore-plugin';
@Injectable({
providedIn: 'root',
})
export class SpotsFirestore extends NgxsFirestore<Spot> {
protected path = 'seasons/2021/spots';
idField = 'spotId'
}
在这种情况下,它将在 属性 spotId
下添加 DocumentId
我使用 NGXS 进行状态管理,还使用 firestore-plugin(https://github.com/ngxs-labs/firestore-plugin)。
如何在我的对象中添加文档的 ID?
我已经创建了我的商店:
import { Injectable } from '@angular/core';
import { NgxsFirestore } from '@ngxs-labs/firestore-plugin';
@Injectable({
providedIn: 'root',
})
export class SpotsFirestore extends NgxsFirestore<Spot> {
protected path = 'seasons/2021/spots';
}
我已经创建了状态和动作:
import { Injectable } from '@angular/core';
import { Emitted, NgxsFirestoreConnect, StreamEmitted } from '@ngxs-labs/firestore-plugin';
import { Action, NgxsOnInit, Selector, State, StateContext } from '@ngxs/store';
import { InitializeSpotsAction } from './spots.actions';
import { SpotsFirestore } from './spots.firestore';
export interface SpotsStateModel {
spots: Spot[];
}
@State<SpotsStateModel>({
name: 'spots',
defaults: {
spots: [],
},
})
@Injectable()
export class SpotsState implements NgxsOnInit {
@Selector()
static spots(state: SpotsStateModel) {
return state.spots;
}
constructor(private spotsFireStore: SpotsFirestore, private ngxsFirestoreConnect: NgxsFirestoreConnect) {}
ngxsOnInit(ctx?: StateContext<any>) {
this.ngxsFirestoreConnect.connect(InitializeSpotsAction, {
to: (action) => this.spotsFireStore.collection$(),
});
}
@Action(StreamEmitted(InitializeSpotsAction))
getAllEmitted(ctx: StateContext<SpotsStateModel>, { action, payload }: Emitted<InitializeSpotsAction, Spot[]>) {
ctx.patchState({ spots: payload });
}
}
我的模型是这样的:
interface Spot {
id: string;
name: string;
description: string;
checkinValue: number;
imageUrl: string;
coordinates: firebase.firestore.GeoPoint;
}
很明显,id 不在我的 firebase 文档中,但正在识别它。不过有了它肯定对我有用
我想我应该重写 NgxsFirestore 中的某些内容,但我不知道如何重写? 是否必须将其包含在文档中?
可以!您只需要在名为 idField
的服务中创建一个 属性,如下所示:
import { Injectable } from '@angular/core';
import { NgxsFirestore } from '@ngxs-labs/firestore-plugin';
@Injectable({
providedIn: 'root',
})
export class SpotsFirestore extends NgxsFirestore<Spot> {
protected path = 'seasons/2021/spots';
idField = 'spotId'
}
在这种情况下,它将在 属性 spotId