【发布时间】:2019-08-30 13:55:01
【问题描述】:
我正在尝试获取将“课程”下的数据添加到 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')
}
【问题讨论】:
标签: angular firebase ionic-framework firebase-realtime-database ionic4