按下后如何检索密钥? Firebase AngularFire
How to retrieve key once you push it? Firebase AngularFire
目前在我的项目中,我正在将对象推送到特定参考中的 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() 本身会自动创建一个密钥吗?),我只想我刚刚推送的课程的关键并设置在另一个参考中,这可能吗?如何?谢谢。
你可以使用 .key() 函数
var dirChatRef = firebaseRef.url.child('directChat').push(chatObj);
Logger.info("New User direct chat Id : " + dirChatRef.key());
Angularfire 将 return 一个有前途的参考。尝试
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);
}
目前在我的项目中,我正在将对象推送到特定参考中的 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() 本身会自动创建一个密钥吗?),我只想我刚刚推送的课程的关键并设置在另一个参考中,这可能吗?如何?谢谢。
你可以使用 .key() 函数
var dirChatRef = firebaseRef.url.child('directChat').push(chatObj);
Logger.info("New User direct chat Id : " + dirChatRef.key());
Angularfire 将 return 一个有前途的参考。尝试
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);
}