I am trying to retrieve an array from my [Firebase] Realtime Database
Here is the code snippet I have written:
add-student.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CurdService } from '../shared/curd.service';
import Swal from 'sweetalert2';
import { Student } from '../shared/student';
@Component({
selector: 'app-add-student',
templateUrl: "./regist.html",
styles: [
]
})
export class AddStudentComponent implements OnInit {
public studentForm: FormGroup;
somthing: any; somestuden: Student[];
avilableor: boolean = true;
data: any[];
some: any[];
constructor(
public crudApi: CurdService,
public fb: FormBuilder,
) {}
ngOnInit() {
this.crudApi.GetStudentsList();
this.studenForm();
this.crudApi.GetStudentsList().snapshotChanges().subscribe(data =>{
this.data = data;
})
console.log(this.data);
}
studenForm() {
this.studentForm = this.fb.group({
username: ['', [Validators.required, Validators.minLength(5)]],
fullname: [''],
email: [''],
note: [''],
});
}
get username() {
return this.studentForm.get('username');
}
get fullname() {
return this.studentForm.get('fullname');
}
get email() {
return this.studentForm.get('email');
}
get note() {
return this.studentForm.get('note');
}
ResetForm() {
this.studentForm.reset();
}
validusername(){
}
}
curd.service.ts
import { Injectable } from '@angular/core';
import { Student } from './student';
import {
AngularFireDatabase,
AngularFireList,
AngularFireObject,
} from '@angular/fire/compat/database';
@Injectable({
providedIn: 'root'
})
export class CurdService {
studentsRef: AngularFireList<any>;
studentRef: AngularFireObject<any>;
constructor (private db: AngularFireDatabase){}
AddStudent(student: Student){
this.studentsRef.push({
username: student.username,
fullname: student.fullname,
email: student.email,
note: student.note,
});
}
GetSudent(id: string){
this.studentRef = this.db.object('students-list/' + id);
return this.studentsRef;
}
GetStudentsList() {
this.studentsRef = this.db.list('students-list');
return this.studentsRef;
}
UpdateStudent(student: Student) {
this.studentRef.update({
username: student.username,
fullname: student.fullname,
email: student.email,
note: student.note,
});
}
DeleteStudent(id: string) {
this.studentRef = this.db.object('students-list/' + id);
this.studentRef.remove();
}
}
Error Message:
undefined
add-student.component.ts:28:12 ERROR TypeError: this.data is undefined
Can anyone provide me with a solution for this issue or suggest an alternative method to fetch data from Firebase realtime database and assign it to a variable?