Jump to content

Javascript anonymous function problem

Go to solution Solved by Maximation,

Solved it.

Thanks to you guys for helping me out. /sarcasm

Basically, see this stackoverflow post for the solution: http://stackoverflow.com/questions/19168440/javascript-anonymous-function-access-to-global-variables
 

Good day,

 

I am experimenting with Firebase and its workings, but now there is some weird stuff occurring. I don't think it has anything to do with Firebase itself. The problem is that somehow the exists variable stays false. When I set it to true in the playerExists function it somehow gets back to false when it is checked. This script basically needs to check if the player already exists, this is the datastructure in Firebase:

{  "players" : {    "Maeb" : {      "name" : "Maeb"    }  }}

See this picture for the web console output (don't worry about the first line of the console, this happens when I refresh the page, it does not affect the connection with firebase, afaik) (edit: sorry for the spelling mistake, 'chose' should be 'choose') :

bupFufF.png

 

It returns false when it should return true, as you can see playerExists executed because it inserted text to let the user know that name is taken.

 

Here is the full code :

/* * *    File : script.js *    Author : maeb *    (c) 2014 * */(function() {	var players = new Firebase("https://cc-charactersheet.firebaseio.com/players/");	var player;	var exists = false;	document.getElementById('create').addEventListener('click', function() {		var player_name = document.getElementById('player_name').value;		if (doesPlayerExist(player_name))			console.log(true);		else			console.log(false);	});	function doesPlayerExist(player_name) {		players.once("value", function(snapshot) {			var data = snapshot.val();			for (var key in data) {				if (key == player_name)					playerExists();			}		}, function(error) {			console.log("Error: " + error);		});		return exists;	}	function playerExists() {		var pli = document.getElementById('player-login-info');		if (pli)			pli.parentElement.removeChild(pli);		exists = true;		var exists_message = document.createElement('p');		exists_message.className = 'four columns offset-by-two red centered';		exists_message.id = 'player-login-info';		exists_message.textContent = 'That player already exists, please choose another name';		document.getElementById('login-form').insertBefore(exists_message, document.getElementById('login'));	}})();

So yeah, that is a quite interesting problem. I hope you can help me out!

Learning

Link to comment
https://linustechtips.com/topic/246309-javascript-anonymous-function-problem/
Share on other sites

Link to post
Share on other sites

Okay, so I narrowed the problematic area down to this:

players.once("value", function(snapshot) {			var data = snapshot.val();			for (var key in data) {				if (key == player_name)					playerExists();			}		}, function(error) {			console.log("Error: " + error);		});

I assume the problem gets a problem because of the anonymous function used as an argument for players.once(). I have no idea how to get it to work though, how can I solve the problem with the anonymous function?

Learning

Link to post
Share on other sites

Solved it.

Thanks to you guys for helping me out. /sarcasm

Basically, see this stackoverflow post for the solution: http://stackoverflow.com/questions/19168440/javascript-anonymous-function-access-to-global-variables
 

Learning

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×