【问题标题】:How to solve "unexpected token : A constructor, method, accessor, or property was expected" error?如何解决“意外的令牌:预期的构造函数、方法、访问器或属性”错误?
【发布时间】:2017-12-01 17:18:42
【问题描述】:

我正在编写一个代码来触发对 firebase 的查询。这是我相关课程的代码 sn-p :

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);
    });





}

现在我得到的错误是"unexpected token : A constructor, method, accessor, or property was expected" in "this.usersRef.orderByChild" 在上述 sn-p 的以下部分:

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

如何解决此错误?请帮忙!

【问题讨论】:

    标签: angular typescript firebase ionic2


    【解决方案1】:

    我不相信 Typescript 允许在类中使用顶级表达式。

    你需要移动

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

    到方法内部。如果您希望它在创建实例时运行,请将其粘贴在构造函数中。否则将其放入某个方法中,并在您希望它运行时调用该方法。

    【讨论】:

    • 我认为你需要使用箭头功能来访问this.list..虽然不是问题的一部分
    • @suraj 老实说,我的 Typescript 太生锈了,不能打那个电话。如果它与 Javascript 相同,我认为你是对的。
    • 是的.. typescript 是 js 的超集.. 严格的类型系统只是额外的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2016-09-12
    • 2016-03-30
    • 2015-10-02
    • 1970-01-01
    • 2018-07-07
    相关资源
    最近更新 更多