How to solve "unexpected token: constructor, method, accessor, or property expected"?

I am writing code to run a request in firebase. This is a code snippet of my respective class:

export class ViewUserPage {
  public list = [];
  public ref = firebase.database().ref();
  public usersRef = this.ref.child('users');

  constructor(public navCtrl: NavController, public navParams: NavParams) {}
  
  
  this.usersRef.orderByChild('tag').equalTo('staff').on('child_added',function(snap){
    this.list.push(snap.val().email);
    });





}
      

Run codeHide result


Now the error I am getting is "unexpected token : A constructor, method, accessor, or property was expected"

in "this.usersRef.orderByChild"

the next part of the snippet above:

this.usersRef.orderByChild('tag').equalTo('staff').on('child_added',function(snap){
    this.list.push(snap.val().email);
    });
      

Run codeHide result


How do I resolve this error? Please, help!

+3


source to share


1 answer


I don't believe Typescript allows top level expressions inside a class.

You need to move



this.usersRef.orderByChild('tag').equalTo('staff').on('child_added',function(snap){
    this.list.push(snap.val().email);
    });

      

inside the method. If you expect it to run on instantiation, insert it into the constructor. Otherwise, put it in some method and call the method when you want to run it.

+2


source







All Articles