user3240644
user3240644

Reputation: 2281

Polymer using firebase query with context

in my custom Polymer element, i have a method that queries a firebase instance like so:

 _updateUser: function(user) {
        if(this._firebaseRef) {
          this._firebaseRef.off()
        }
        if(user) {

          var userLocation = ['xxxxx.firebaseio.com', 'users', this.user.uid].join('/');
          this._firebaseRef = new Firebase(userLocation);
          var query = this._firebaseRef.orderByChild("username")
          query.on("value", function(messageSnapshot) {
          messageSnapshot.forEach(function(messageData){
              this.set('usernames', []);
              console.log(this);
            });
          },null, this);

        }
      },

However, I keep getting the following error Uncaught TypeError: this.set is not a function. It seems that this is not set as the context as expected as per the api documentation

May I know how I can call this.set in my callback successfully? Thanks.

Upvotes: 0

Views: 379

Answers (1)

Scarygami
Scarygami

Reputation: 15549

You need to pass the context into the forEach function as well.

messageSnapshot.forEach(function (messageData) {...}, this)

or

messageSnapshot.forEach(function (messageData) {...}.bind(this))

Upvotes: 3

Related Questions