Issue
I want to return array from [Firebase] Realtime Database
This is my code:
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 someone tell me, how to solve this problem or is there any other way to return data from firebase realtime database to my variable?
Solution
You need to declare data
export class HelloComponent implements OnInit {
data = [];
....
]
Answered By - Myat Thiha
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.