[AngularFire2] Update multi collections at the same time with FirebaseRef
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));
相关文章
- ORA-01311: Illegal invocation of the mine_value function ORACLE 报错 故障修复 远程处理
- ORA-28013: the password has expired ORACLE 报错 故障修复 远程处理
- ORA-38430: Operation “string” not supported in the current release. ORACLE 报错 故障修复 远程处理
- ORA-39317: Cannot change the segment space management of a tablespace. ORACLE 报错 故障修复 远程处理
- ORA-41671: duplicate table names in the event structure with table aliases ORACLE 报错 故障修复 远程处理
- ORA-48408: The incident number exceeds the maximum number [string] ORACLE 报错 故障修复 远程处理
- ORA-48444: The single “.” and “*” cannot appear in the middle of the path ORACLE 报错 故障修复 远程处理
- ORA-53253: The metadata for the new DICOM object is invalid. ORACLE 报错 故障修复 远程处理
- ORA-56714: Plan name string exceeds the maximum length allowed ORACLE 报错 故障修复 远程处理
- ORA-03261: the tablespace string has only one file ORACLE 报错 故障修复 远程处理
- ORA-13708: Some snapshots in the range [string, string] were purged before the analysis was complete. ORACLE 报错 故障修复 远程处理
- ORA-15155: version incompatible with the cluster ORACLE 报错 故障修复 远程处理
- mysql8.0 设置简单密码报错ERROR 1819 (HY000): You’re password does not satisfy the current policy requirements详解程序员
- How to expand the BOM using ABAP详解编程语言
- what is the difference of select single and select up to one row in abap详解编程语言
- 系统Ubuntu——The Most Popular Linux Distribution System(linux的分支)
- Unlock the Power of MySQL with Qt(mysqlqt)
- Unlocking the Power of Data with Linux: A Comprehensive Guide(linuxdata)
- Exploring the Power of ARM Architecture with MySQL Database Management(armmysql)
- Exploring the Power of Linux with C Code: Tips Tricks and Best Practices(linuxc代码)
- Mastering the Power of Alias in Linux: Simplify Your Workflow Today!(aliaslinux)
- Exploring the Architecture of MongoDB: A Visual Guide(mongodb架构图)
- Exploring the Power of Linux with Hydra: A Comprehensive Guide to Network Security(linuxhydra)
- Unlock the Potential of Your Business with Linux Scale Solutions(linuxscale)
- 语句使用Oracle两个WITH语句实现数据查询(oracle两个with)