import {Injectable} from '@angular/core';
import {Skill} from '../models/skills';
import {AuthService} from '../../auth/services/auth.service'; import 'rxjs/add/operator/do';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/operator/catch';
import {Observable} from 'rxjs/Observable';
import {AngularFirestore, AngularFirestoreCollection} from 'angularfire2/firestore'; @Injectable()
export class SkillsService { skillCollection: AngularFirestoreCollection<Skill>;
skills: Observable<Skill[]>;
events: Observable<any>; constructor(private afs: AngularFirestore,
private authService: AuthService) { this.skillCollection = afs.collection(`users/${this.uid}/skills`,
ref => ref.orderBy('name').limit(10));
this.events = this.skillCollection.auditTrail();
} get uid() {
return this.authService.user.uid;
} getSkills(): Observable<Skill[]> {
return this.skillCollection
.snapshotChanges()
.startWith([]) // To solve the snapshotChange doesn't fire for empty data
.map((actions) =>
actions.map(({payload}) => ({id: payload.doc.id, ...payload.doc.data()} as Skill))
);
} getSkill(key): Observable<Skill> {
return this.afs.doc(`users/${this.uid}/skills/${key}`)
.snapshotChanges()
.map(({payload}) => ({id: payload.id, ...payload.data()} as Skill));
} addSkill(skill: Skill) {
return this.skillCollection.add(skill);
} updateSkill(key: string, skill: Partial<Skill>) {
return this.afs.doc<Skill>(`users/${this.uid}/skills/${key}`).update(skill);
} removeSkill(key: string) {
return this.afs.doc<Skill>(`users/${this.uid}/skills/${key}`).delete();
} }

[AngularFire2 & Firestore] Example for collection and doc的更多相关文章

  1. [AngularFire] Firebase OAuth Login With Custom Firestore User Data

    import { NgModule } from '@angular/core'; import { AuthService } from './auth.service'; import { Ang ...

  2. doc.update

    db.collection('todos').doc('todo-identifiant-aleatoire').update({ // data 传入需要局部更新的数据 data: { // 表示将 ...

  3. 微信小程序云数据库——where查询和doc查询区别

    用法 条件查询where 我们也可以一次性获取多条记录.通过调用集合上的 where 方法可以指定查询条件,再调用 get 方法即可只返回满足指定查询条件的记录,比如获取用户的所有未完成的待办事项,用 ...

  4. React Native纯干货总结

    随着项目也渐渐到了尾声,之前的项目是mobile开发,采用的是React Native.为即将要开始做RN项目或者已经做过的小伙伴可以参考借鉴,也顺便自己做一下之前项目的总结. 文章比较长,可以选择自 ...

  5. MongoDB Java Driver操作指南

    MongoDB为Java提供了非常丰富的API操作,相比关系型数据库,这种NoSQL本身的数据也有点面向对象的意思,所以对于Java来说,Mongo的数据结构更加友好. MongoDB在今年做了一次重 ...

  6. MongoDB 聚合操作

    在MongoDB中,有两种方式计算聚合:Pipeline 和 MapReduce.Pipeline查询速度快于MapReduce,但是MapReduce的强大之处在于能够在多台Server上并行执行复 ...

  7. [译] MongoDB Java异步驱动快速指南

    导读 mongodb-java-driver是mongodb的Java驱动项目. 本文是对MongoDB-java-driver官方文档 MongoDB Async Driver Quick Tour ...

  8. 10 行 Python 代码写的模糊查询

    导语: 模糊匹配可以算是现代编辑器(在选择要打开的文件时)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的文件名,并提供一个推荐列表供用户选择. 样例如下: Vim (Ctrl-P ...

  9. MongoDB C Driver使用教程

    MongoDB C Driver使用教程 转载请注明出处http://www.cnblogs.com/oloroso/ 本指南提供简介 MongoDB C 驱动程序. 在 C API 的详细信息,请参 ...

随机推荐

  1. eBay起诉指控亚马逊利用非法手段挖走其卖家

    [摘要]eBay在诉状中称,亚马逊的代表滥用eBay的内部电子邮件系统联系卖家,这违反了市场政策. 腾讯科技讯 10月18日消息,据外媒报道,拍卖网站eBay对亚马逊提起诉讼,指控这家美国零售巨头利用 ...

  2. 题解 P2504 【[HAOI2006]聪明的猴子】

    这道题要坑死人啊... 第一次做40分,其他RE,改了一次之后就变成20分了... 究其原因,是有一个数组(dis)开的太小了,于是最后本蒟蒻就随手开了个五百万,然后,就AC了. 看到有一篇题解说求距 ...

  3. 【CS Round #36 (Div. 2 only) A】Bicycle Rental

    [题目链接]:https://csacademy.com/contest/round-36/task/bicycle-rental/ [题意] 让你从n辆车中选一辆车; 每一辆车有3个属性 1.到达车 ...

  4. 137 - ZOJ Monthly, November 2014 - J Poker Face

    Poker Face Time Limit: 2 Seconds      Memory Limit: 65536 KB As is known to all, coders are lack of ...

  5. OpenStack依然大行其道,可是否能等来属于自己的时代

    版权声明:不论什么转载需全文转载并保留来源(微信公众号techculture)和该声明,并同一时候转载文后的二维码.否则视作侵权. OpenStack在云计算领域依然火热,甚至能够说势不可挡.这可能是 ...

  6. POJ 1152 An Easy Problem! (取模运算性质)

    题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R.保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现当中将N进制话成10进制时,数据会溢 ...

  7. setTimeOut函数传参数

    这样使用,后面的4000无效 setTimeout(removeGift(customer_id,gift_id),4000); function removeGift(customer_id,gif ...

  8. CORS support in Spring Framework--官方

    原文地址:https://spring.io/blog/2015/06/08/cors-support-in-spring-framework For security reasons, browse ...

  9. UESTC 30最短路(flyod算法)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. centos 5的yum源无法使用的解决方法( 转载)

    由于centos 5 已经停更.于是导致yum源也不能用了. 例如安装screen的时候提示 Determining fastest mirrors* base: denver.gaminghost. ...