【问题标题】:How to retrieve key once you push it? Firebase AngularFire按下后如何检索密钥? Firebase AngularFire
【发布时间】:2018-08-21 16:33:32
【问题描述】:

目前在我的项目中,我正在将对象推送到特定参考中的 firebase:

this.CoursesList = this.mydb.database.ref('/courses/' + this.CourseCategoryForm);
this.CoursesList.push(this.course);
this.Reference();

但我想同时将它推到其他地方。我读到无法在 Firebase 中进行多重推送,因此我编写了函数 Reference 来根据其状态推送课程。

Reference(){
    if(this.CourseStatusDecision == 0){
        this.CoursesList = this.mydb.database.ref('/availableCourses/' + this.CourseCategoryForm);
    }else{
        this.CoursesList = this.mydb.database.ref('/currentSemester/');
    }
    this.CoursesList.set();
}

我的问题是我不想在另一个 ref 中再次推送整个对象(它通常使用不同的键推送,是因为 .push() 本身会自动创建一个键吗?),我只想要键当然,我只是将其推送并设置在另一个参考中,这可能吗?如何?谢谢。

【问题讨论】:

标签: angular typescript firebase firebase-realtime-database angularfire


【解决方案1】:

Angularfire 将返回一个带有承诺的引用。试试

this.CoursesList = this.mydb.database.ref('/courses/' + this.CourseCategoryForm);
this.CoursesList.push(this.course).then(ref=>{
    console.log(ref.key);
    //pass the key to the function
    this.Reference(ref.key);
}

【讨论】:

    【解决方案2】:

    你可以使用 .key() 函数获得

    var dirChatRef = firebaseRef.url.child('directChat').push(chatObj);
    
    Logger.info("New User direct chat Id : " + dirChatRef.key());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 2020-07-13
      • 1970-01-01
      • 2018-11-17
      • 2016-11-06
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多