Total Pageviews

Tuesday, July 18, 2017

Cannot set variable of undefined

https://stackoverflow.com/questions/38807643/error-uncaught-in-promise-typeerror-cannot-set-property-isadmin-of-null

when variable not set in function "then" block, this doesnt work, need to a copy of "this" reference with in the calling function.



down voteaccepted
Either you can use arrow function
firebase.database().ref('/userProfile/' + user.uid).once('value')
.then((snapshot) => {
or use
var self = this;

firebase.database().ref('/userProfile/' + user.uid).once('value')
.then(function(snapshot) {
   self.isAdmin = true;
otherwise this doesn't point to the current function when it's called.

No comments: