1

Currently in my project I am pushing the object to firebase in a specific ref:

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

But I want to push it somewhere else at the same time. I read that it was not possible to multipush in Firebase so I wrote the function Reference that would push the course depending on its status.

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();
}

My question is I don't want to push the entire object again in another ref (and it usually pushes with a different key, is that because .push() itself creates a key automatically?), I just want the key of the course I just pushed and set it in the other reference, is that possible? And how? Thank you.

André Kool
  • 4,880
  • 12
  • 34
  • 44
Kode Ch
  • 139
  • 1
  • 1
  • 12
  • Possible duplicate of [Firebase: Can I combine a push with a multi-location update?](https://stackoverflow.com/questions/36774143/firebase-can-i-combine-a-push-with-a-multi-location-update) – André Kool Mar 13 '18 at 13:09

2 Answers2

3

Angularfire will return a reference with promise. Try

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);
}
Hareesh
  • 6,770
  • 4
  • 33
  • 60
0

you can get using .key() function

var dirChatRef = firebaseRef.url.child('directChat').push(chatObj);

Logger.info("New User direct chat Id : " + dirChatRef.key());
rijin
  • 1,709
  • 12
  • 21