我试图获取在“ Course”下将数据添加到Firebase时生成的随机密钥。我正在使用Ionic 4。
[1] https://imgur.com/a/mcN38Fz
在下面的代码中,在onSubmit函数中,最后两行: 我有一个变量,它持有用户的UID,称为“ currentUser”,另一个变量,应持有添加的数据键“ datakey”。我不确定“ dataKey”变量的完整代码应该是什么,以便我可以获取所添加数据的密钥。
有什么帮助吗?
import { Component, OnInit } from '@angular/core';
import { Router,ActivatedRoute } from '@angular/router';
import { Validators, FormBuilder, FormGroup, FormControl } from '@angular/forms';
import { FirebaseService } from '../services/firebase.service';
import { AuthService } from '../services/auth.service';
import { AngularFirestore } from '@angular/fire/firestore';
import * as firebase from 'firebase/app';
import { LoadingController } from '@ionic/angular';
@Component({
selector: 'app-subjects',
templateUrl: './subjects.page.html',
styleUrls: ['./subjects.page.scss'],
})
export class SubjectsPage implements OnInit {
subject_form: FormGroup;
subjects: Array<any>;
gradeArray: number[]=[];
creditHourArray: number[]=[];
creditHourTot: number=0;
gpaDisplay: number;
dataKey: string;
constructor(
private authService: AuthService,
private formBuilder: FormBuilder,
private firebaseService: FirebaseService,
private router: Router,
private route: ActivatedRoute,
public loadingCtrl: LoadingController,
public afs: AngularFirestore,
) { }
ngOnInit() {
if (this.route && this.route.data) {
this.getData();
}
this.resetFields();
}
async getData(){
this.route.data.subscribe(routeData => {
routeData['data'].subscribe(data => {
this.subjects = data;
})
})
}
async presentLoading(loading) {
return await loading.present();
}
resetFields() {
this.subject_form = this.formBuilder.group({
subName: new FormControl('', Validators.required),
subCode: new FormControl('', Validators.required),
creditHour: new FormControl('', Validators.required),
grade: new FormControl('', Validators.required)
});
}
onSubmit(value) {
this.gradeArray.push(value.grade)
this.creditHourArray.push(value.creditHour)
let data = {
subName: value.subName,
subCode: value.subCode,
creditHour: value.creditHour,
grade: value.grade,
}
this.creditHourTot= this.creditHourTot + value.creditHour;
this.firebaseService.createSubjects(data,this.gradeArray,this.creditHourArray)
this.firebaseService.createGPA(this.gradeArray,this.creditHourArray)
this.gpaDisplay = this.firebaseService.calGPA(this.gradeArray,this.creditHourArray)
console.log(this.gpaDisplay);
let currentUser = firebase.auth().currentUser
let dataKey = this.afs.collection('people').doc(currentUser.uid).collection('Course')
}
goToSemPage() {
this.router.navigate(["/semesters"]);
}
goToSubListPage(){
this.router.navigate(["/subject-list"]);
}
logout() {
this.authService.doLogout()
.then(res => {
this.router.navigate(["/home"]);
}, err => {
console.log(err);
})
}
}
这是我的方法“ createSubjects”的代码
createSubjects(value,gradeArray,creditHourArray) {
return new Promise<any>((resolve, reject) => {
let currentUser = firebase.auth().currentUser;
this.afs.collection('people').doc(currentUser.uid).collection('Course').add({
subName: value.subName,
subCode: value.subCode,
creditHour: value.creditHour,
grade: value.grade
gpa: this.calGPA(gradeArray,creditHourArray)
})
.then(
res => resolve(res),
err => reject(err)
)
})
}
更新:新的“ onSubmit”方法:
onSubmit(value) {
this.gradeArray.push(value.grade)
this.creditHourArray.push(value.creditHour)
let data = {
subName: value.subName,
subCode: value.subCode,
creditHour: value.creditHour,
grade: value.grade,
}
this.creditHourTot= this.creditHourTot + value.creditHour;
this.firebaseService.createSubjects(data,this.gradeArray,this.creditHourArray).then(
(val) => { this.datakey = val.id }
);
this.firebaseService.createGPA(this.gradeArray,this.creditHourArray)
this.gpaDisplay = this.firebaseService.calGPA(this.gradeArray,this.creditHourArray)
console.log(val);
// let currentUser = firebase.auth().currentUser
// let datakey = this.afs.collection('people').doc(currentUser.uid).collection('Course')
}
答案 0 :(得分:1)
我不确定您对用例的理解。
我假设您的方法“ createSubjects”是调用firebase创建“课程”的方法。
因此,您需要在方法“ createSubject”中返回一个承诺,
如果createSubject直接返回Firebase Promise,那么您只需:
this.firebaseService.createSubjects(data,this.gradeArray,this.creditHourArray).then(
(val) => { this.datakey = val.id }
);
编辑:我将'key'更改为'id',密钥是您在Firebase上获得的内容,在Firestore中使用了'id'。