At some point, you might need to udpate multi collections and those collections should all updated successfully, otherwise we don't update anything. You can use 'FirebaseRef' to get the root node (the parent all for the collections).

  constructor(private rt: RealtimeService, @Inject(FirebaseRef) fb) {

    this.rootDb = fb.database().ref(); // To get the root firebase ref
}

You can inject 'FirebaseRef' by using @Inject.

For example we want to update 'lessons' and 'lessonsPreCourse' at the same time, make sure if both sucess, both get udpated, one has error then abort the transaction:

To add value into lessonsPerCourse key, we need to grap the new Lesson key first.

To do that, we can generate a new lesson key by doing:

const newLessonKey = this.rootDb.child('lessons').push().key;

This won't actually insert a row into the 'lessons' table, it just create a new key for lessons collection for us to use locally.

Once we got the new lesson key, then we can update the table by calling 'update()' on 'this.rootDb':

this.rootDb.update(dataToSave)
.then( (val) => { }, (err) => { });

The full code for create new lesson:

  createNewLesson(courseId: string, lesson: Lesson): Observable<any>{
const lessonToSave = Object.assign({}, lesson, {courseId}); // Generate a new key under 'lessons' collection, db won't change currently
const newLessonKey = this.rootDb.child('lessons').push().key; const dataToSave = {};
dataToSave[`lessons/${newLessonKey}`] = lessonToSave;
dataToSave[`lessonsPerCourse/${courseId}/${newLessonKey}`] = true; const subject = new Subject();
this.rootDb.update(dataToSave)
.then( (val) => {
subject.next(val);
subject.complete();
}, (err) => {
subject.error(err);
subject.complete();
});
return subject.asObservable();
}

Notice that, we also use Subject(), because the function expect to return an Observable. So we call:

return subject.asObservable();

Also we call complete() method everytime, this is important to complete a subject.

subject.complete();

To mention that, subject is not the only way to can convert Promise to Observable, we can also do:

return Observable.fromPromise(this.rootDb.update(dataToSave));

[AngularFire2] Update multi collections at the same time with FirebaseRef的更多相关文章

  1. 【网络爬虫入门05】分布式文件存储数据库MongoDB的基本操作与爬虫应用

    [网络爬虫入门05]分布式文件存储数据库MongoDB的基本操作与爬虫应用 广东职业技术学院  欧浩源 1.引言 网络爬虫往往需要将大量的数据存储到数据库中,常用的有MySQL.MongoDB和Red ...

  2. mongoDB的安装及基本使用

    1.mongoDB简介 1.1 NoSQL数据库 数据库:进行高效的.有规则的进行数据持久化存储的软件 NoSQL数据库:Not only sql,指代非关系型数据库 优点:高可扩展性.分布式计算.低 ...

  3. MongoDB基础一篇就够了

    MongoDB linux安装MongoDB Windows安装MongoDB 查看当前数据库名称 db 查看所有数据库名称 列出所有在物理上存在的数据库 show dbs 切换数据库 如果数据库不存 ...

  4. mongodb细讲

    一. 关系型数据库(sql) 1.建表 二.非关系型数据库(nosql  98提出的概念) 1.不用建库建表数据直接存入就可 优缺点: 关系型:节约资源(学生姓名和课程名不重复出现),开发不方便(需先 ...

  5. mongo 数据库

    一.管理mongo 配置文件在/etc/mongod.conf 默认端口27017 启动                    sudo service mongod start 停止         ...

  6. python数据库进阶

    第1节 MySQL基础 一,说明 1,认识MySQL与创建用户 MySQL是最流行的关系型数据库管理系统之一,由瑞典MySQL AB公司开发,目前属于Oracle公司.MySQL是一种关联数据管理系统 ...

  7. [转] mongoDB与mongoose

    mongoDB简介 mongoDB与一些关系型数据库相比,它更显得轻巧.灵活,非常适合在数据规模很大.事务性不强的场合下使用.同时它也是一个对象数据库,没有表.行等概念,也没有固定的模式和结构,所有的 ...

  8. MongDB-基础

    首先吐槽一下,MongDB用到了JS的引擎,只要涉及到了JS,语法就变得又臭又长,真是无语 还有,MongDB的安装真是麻烦,我用的是win10环境,怎么装都报服务错误,redis一装就可以用,希望m ...

  9. MangoDB学习笔记

    01. 数据库操作 1. 查看当前数据库名称 db 2. 查看所有数据库名称,列出所有在物理上存在的数据库 show dbs; 3. 切换数据库,如果数据库不存在也并不创建,直到插入数据或创建集合时数 ...

随机推荐

  1. 【Codeforces Round #456 (Div. 2) C】Perun, Ult!

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] set1 < pair < int,int > > set1;记录关键点->某个人怪物永远打不死了,第 ...

  2. HTTP (httpwebrequest)

    1.GET请求: public static string Get(string url) { string buffer = ""; try { HttpWebRequest r ...

  3. [Android] 图像各种处理系列文章合集

        这是我近期在做Android随手拍一个项目的各种网上关于图片处理的资料,曾经学过数字图像处理都是用C++写的,以下的资料个人觉得是很优秀的各种集合,还有一方面它是在线笔记,希望对大家有所帮助吧 ...

  4. 洛谷 P1054 等价表达式

    洛谷 P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式, ...

  5. BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings

    继上一节.BeautifulSoup的高级应用 之 find findAll,这一节,主要解说BeautifulSoup有关的其它几个重要应用函数. 本篇中,所使用的html为: html_doc = ...

  6. bootstrap课程7 jquery中结束之前动画用什么

    bootstrap课程7 jquery中结束之前动画用什么 一.总结 一句话总结:stop()方法.$('.navs').not($('.navs').eq(idx)).stop().hide(100 ...

  7. redhat6.5安装10201解决办法

    rpm --import /etc/pki/rpm-gpg/RPM*yum install -y  --skip-broken compat-libstdc++* elfutils-libelf* g ...

  8. 笔记二:JS的输出、语法、语句、字符串、条件语句、switch语句、for循环、while循环

    1.JS的输出: 注意:JS没有任何打印或者输出的函数 JS输出数据的集中方法:  1.使用window.alert()弹出警告框: 2.使用document.write()方法将内容写到HTML文档 ...

  9. 学习笔记:_lodash.js常用函数2

    _.pick(object, [props]) 创建一个从object中选中的属性的对象. 示例: var object = { 'a': 1, 'b': '2', 'c': 3 }; _.pick( ...

  10. 网络博客 VC\图案像处理

    http://blog.csdn.net/lvwx369/article/category/1185452 http://blog.csdn.net/lyy289065406/article/deta ...