如何在 Angular 5 的 Firestore 集合中包含文档 ID [英] How to include the document id in Firestore collection in Angular 5

查看:24
本文介绍了如何在 Angular 5 的 Firestore 集合中包含文档 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个获取所有帖子的函数,我想获取帖子的 id知道如何获取帖子的 id 或如何在将文档添加到集合时包含 id

get_the_posts(){this.Post_collection = this.afs.collection('posts');返回 this.Posts = this.Post_collection.valueChanges();}

这给了我这个输出:

<预><代码>[{ name: 'post 1 name' description: 'post description'},{ name: 'post 2 name' description: 'post description'},{ name: 'post 3 name' description: 'post description'},]

我想要这个输出

<预><代码>[{ id: 'm9mggfJtClLCvqeQ', name: 'post 1 name' description: 'post description'},{ id: 'm9mggfJtCldsadaf', name: 'post 2 name' description: 'post description'},{ id: 'm9mggfJtCdsasavq', name: 'post 3 name' description: 'post description'},]

解决方案

.valueChanges() 只返回数据,没有任何元数据.你可以使用 .snapshotChanges()

this.Post_collection = this.afs.collection('posts');返回 this.Posts = this.Post_collection.snapshotChanges().map(actions => {返回 actions.map(a => {const data = a.payload.doc.data();const id = a.payload.doc.id;返回 { id, ...数据};});});

也导入 import { Observable } from 'rxjs/Observable';

对于您的第二个问题,如何在将数据推送到 firestore 时添加 id.试试

//从firestore获取id让 id = this.afs.createId();这个.post = {身份证:身份证,姓名:'姓名',描述:'描述'}this.afs.collection('posts').doc(id).set(this.post).then();

现在您可以使用 .valueChanges() 来获取数据.

I have this function that gets all the posts and I want to get the id of the post any idea how to get the id of the post or How to include the id when I add the document to the collection

get_the_posts(){
    this.Post_collection = this.afs.collection('posts'); 
    return this.Posts = this.Post_collection.valueChanges();
}

That gives me this output:

[
   { name: 'post 1 name' description: 'post description'}, 
   { name: 'post 2 name' description: 'post description'},
   { name: 'post 3 name' description: 'post description'},
]

And i want this output

[
   { id: 'm9mggfJtClLCvqeQ', name: 'post 1 name' description: 'post description'}, 
   { id: 'm9mggfJtCldsadaf', name: 'post 2 name' description: 'post description'},
   { id: 'm9mggfJtCdsasavq', name: 'post 3 name' description: 'post description'},
]

解决方案

.valueChanges() returns only data without any meta data. you can use .snapshotChanges() for that

this.Post_collection = this.afs.collection('posts');
return this.Posts = this.Post_collection.snapshotChanges().map(actions => {
  return actions.map(a => {
    const data = a.payload.doc.data();
    const id = a.payload.doc.id;
    return { id, ...data };
  });
});

also import import { Observable } from 'rxjs/Observable';

For your second question how to add id when pushing data to firestore. try

//get id from firestore
    let id = this.afs.createId();

    this.post = {
      id:id,
      name:'name',
      description:'description'
    }

    this.afs.collection('posts').doc(id).set(this.post).then();

Now you can use .valueChanges() to get the data.

这篇关于如何在 Angular 5 的 Firestore 集合中包含文档 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆