使用 ngxs-lab/firestore-plugin 时如何获取对象内的 firebase 文档 ID

J4N

我使用 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'
}

在这种情况下,它将在属性下添加 DocumentId spotId

参考:https : //github.com/ngxs-labs/firestore-plugin

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Firebase Flutter获取文档ID

Firebase + Kotlin,查找文档ID并使用ID删除文档

使用 React JS 在 Firebase v9 中创建文档时保存文档 ID

如何使用用户定义的文档ID创建Firebase Firestore文档?

如何在Firebase云功能中使用adminSdk为Firestore文档创建文档ID?

Flutter:如何使用foreach从Firebase获取数据以从文档中恢复ID

Flutter Firebase通过ID数组获取文档

Firebase 和 Kotlin:获取文档问题的 id

Firebase Firestore:获取生成的文档ID(Python)

Firebase 获取嵌套多个分支的文档 ID

使用FirestoreRecyclerAdapter时如何获取Firestore文档ID?

如何使用Kotlin数据类获取Firestore文档的文档ID

如何获取插入文档 firebase-admin 的 id

react-redux-firebase - 如何使用一组 ID 从 Firestore 查询文档?

如何使用firebase firestore flutter的自动生成的ID删除集合中的特定文档?

您如何使用 Firebase JS SDK 的第 9 版通过 ID 查找文档?

使用自动生成的ID更新Firebase中的文档

使用文档自动ID Firebase查询“ where”子句

使用 swift 按文档 ID 查询 Firebase Firestore

如何从firebase获取文档?

Firestore,使用快照获取文档ID

使用Decodable在Firestore中获取对象和文档ID

Couchbase:如何使用文档ID模式删除多个文档

如何使用Cloud Functions将文档ID写入文档本身?

如何在AngularFire中使用文档ID查询文档?

如何使用文档ID从mongoDB集合中删除文档?

在Firebase Cloud Functions(Firestore)中获取文档ID

在 firebase 函数中获取新创建的文档 ID 错误

使用 $in 时仅获取每个 ID 上次创建的文档