0
I am developing an angular application, and I need to read the recorded data in my firebase database.
How would I read all the data that is inside object 9 for example ? I need to read this data and write it into variables, so I can compare it to other values.
Follow the code of my Komponent.ts:
import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material';
import { QuestionarioService } from 'src/app/providers/questionario.service';
import { MenuService } from 'src/app/providers/menu.service';
import { FirebaseService } from 'src/app/modules/utils/providers/firebase.service';
import { SessionService } from 'src/app/modules/utils/providers/session.service';
import { Http, Response } from '@angular/http';
@Component({
selector: 'app-questionario',
templateUrl: './questionario.component.html',
styleUrls: ['./questionario.component.sass']
})
export class QuestionarioComponent implements OnInit {
firebase:any;
valorR:number = 0;
valorG:number = 0;
valorB:number = 0;
valorA:number = 0;
valorS:number = 0;
valorI:number = 0;
constructor(public questionario:QuestionarioService,
public menu:MenuService,
public dialog: MatDialog,
public session: SessionService,
private http: Http) {
this.firebase = new FirebaseService();
}
ngOnInit() {
window.scrollTo(0,0);
let url = ''
this.http.get( url )
.pipe( (data: Response) => data.json() ) /* Necessário converter para JSON */
.subscribe( answers => console.log( answers ) );
}
getRadioValorR(event){
if(event.value > this.valorR){
this.valorR = event.value;
}
}
getRadioValorB(event){
if(event.value > this.valorB){
this.valorB = event.value;
}
}
getRadioValorA(event){
if(event.value > this.valorA){
this.valorA = event.value;
}
}
getRadioValorS(event){
if(event.value > this.valorS){
this.valorS = event.value;
}
}
getRadioValorI(event){
if(event.value > this.valorI){
this.valorI = event.value;
}
}
derteminaG(){
this.valorG = this.valorR;
if(this.valorG < this.valorB){
this.valorG = this.valorB;
}
if(this.valorG < this.valorA){
this.valorG = this.valorA;
}
if(this.valorG < this.valorS){
this.valorG = this.valorS;
}
if(this.valorG < this.valorI){
this.valorG = this.valorI;
}
return this.valorG;
}
getClass0(){
if(this.derteminaG() == 0){
return true;
}
}
getClass1(){
if(this.derteminaG() == 1){
return true;
}
}
getClass2(){
if(this.derteminaG() == 2){
return true;
}
}
getClass3(){
if(this.derteminaG() == 3){
return true;
}
}
Desabilitar(element){
var radio = document.getElementById(element) as HTMLInputElement;
if(radio.style.display == 'none'){
radio.style.display = 'table-cell';
}else radio.style.display = 'none';
}
}
If I want to return only object 9 for example, I just change Answers for 9 ?
– JV M
As the return will be an object you can access through the key of the object. In this case, this.answers. 9.
– Beto Silva
The following error was returned after I entered your code : "message": "The json property does not exist in type 'Observable<Response>'.
– JV M
Is not working :/
– JV M
Ok, on the line where you have . pipe( date => data.json() replace with this: . pipe( (date:any) => data.json() ) The correct is to add the type Sponse on return, but I will update the code more calmly soon
– Beto Silva
I replaced it and the error disappeared, but once the page is loaded and the code is called inside ngOnInit(), the application breaks ...
– JV M
This is the error returned : ERROR Error: Uncaught (in Promise): Error: Staticinjectorerror(Appmodule)[Questionariocomponent -> Http]: Staticinjectorerror(Platform: core)[Questionariocomponent -> Http]: Nullinjectorerror: No Provider for Http! Error: Staticinjectorerror(Appmodule)[Questionariocomponent -> Http]: Staticinjectorerror(Platform: core)[Questionariocomponent -> Http]: Nullinjectorerror: No Provider for Http! at Nullinjector.push.. /node_modules/@angular/core/fesm5/core.js.Nullinjector.get
– JV M
Maybe there’s some other logic to your code that conflicts with what I suggested. Ideally you edit your question and add what you have there to make it easier to help.
– Beto Silva
@JVM Ah, there’s one more thing. You need to import the HTTP module inside your App.module.ts I will update the response to make it better to read, okay? Take a look in about 2 minutes.
– Beto Silva
@JVM Code updated. I believe it will now work!
– Beto Silva
Returned an error on top of Response. I’ll update the question q post my code to see if you can help me.
– JV M
I don’t know if the error is only in the example you pasted or in your code, but you need to add your URL and organize the code this way:
let url = "SUA URL"
 this.http.get( url ) .pipe( (data: Response) => data.json() ) 
 .subscribe( 
 answers => console.log( answers ) ); }
– Beto Silva
I’m going to need to get out here now, unfortunately. I hope I’ve helped up to a certain point. I’m realizing that you’re having quite a hard time with basic Angular concepts. I suggest looking for some online course to follow up, it will be more productive for you. Good luck and do not give up!
– Beto Silva
I just took the URL to post the code here, but thank you so much for the help !
– JV M