zoukankan      html  css  js  c++  java
  • [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));
  • 相关阅读:
    .NET Windows服务开发流程
    SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
    SQL触发器批量删除数据库中的表
    如何使用Chrome Timeline 工具(译)
    修复运行 tasklist 命令时提示 ERROR: Not found
    删除坏掉的 Active Directory Domain
    为 WSUS 服务器定期运行清理向导
    为 Exchange 服务器编写自定义的反垃圾插件
    Windows 10 下安装 npm 后全局 node_modules 和 npm-cache 文件夹的设置
    在 Win10 命令行使用 Consolas + 微软雅黑
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6049506.html
Copyright © 2011-2022 走看看