Firebase数据即使包含内容,也会返回UNDEFINED值 [英] Firebase data returning an UNDEFINED value even though it has content

查看:256
本文介绍了Firebase数据即使包含内容,也会返回UNDEFINED值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自我的最近的问题(在我使用Promise技术之前),我想出了一个将这个变量存储在firebase数据库中的想法,而不是把它变成JavaScript中的全局变量,这样我就可以在任何地方使用它函数(即使在一个异步的)。所以我做了这个代码:



function logIn(){var loggedUname = $ ( #loginUname)VAL(); var loggedPword = $(#loginPword)。val(); var tailorRef = firebase.database()。ref('tailors'); var keyRef = firebase.database()。ref(); tailorRef.on('value',function(data){data.forEach(function(childData){if((loggedUname == childData.val()。tUsername)&&(loggedPword == childData.val())。 tPassword)){currentKey = childData.key;}}); keyRef.update({currentTailor:currentKey}); alert(key =+ keyRef.currentTailor);}); }

.update方法成功,因为它存储了相应的键对于登录用户:



更新的数据库

但是当我尝试使用alert检索它时​​,它说UNDEFINED!我不明白,因为我假设我从数据库的根引用调用,这是我一样低。



我试着做一个单独的.on()函数为keyRef,但它仍然是相同的结果。



keyRef.on('value',function(data){alert(data.currentTailor);});



我是否以错误的方式调用了数据?

解决方案

正确的方法是:
$ b $

keyRef.on('value',function(data){
// alert(data.val()。currentTailor);查看差异
console.log('currentTailor == >',data.val()。currentTailor);
});

alert()因为它冻结了你的页面。改用控制台。这样你就可以在浏览器的控制台中看到你的日志了(在chrome中按下 Ctrl + Shift + J


Firebase的 on()方法返回回调函数。要获取数据对象,必须对该返回值使用 val()方法。然后你可以使用相应的键获得任何值。



  keyRef.on('value',函数(数据){
//这里数据是回调函数
});
$ b keyRef.on('value',function(data){
//data.val()以对象形式返回数据
});
keyRef.on('value',function(data){
// data.val()。currentTailor来访问currentTailor属性
});

您可以控制每个人看看返回值是什么。 b

Coming from my recent question in this site (before I resort to the "Promise" technology), I came up an idea of storing the variable in the firebase database instead of making it a global var in the javascript so that I can use it in any function (even though in an asynchronous one). So I made this code:

function logIn(){
	var loggedUname = $("#loginUname").val();
	var loggedPword = $("#loginPword").val();
	
	var tailorRef = firebase.database().ref('tailors');
	var keyRef = firebase.database().ref();
	
	tailorRef.on('value', function(data){
		data.forEach(function(childData){
			if ( (loggedUname == childData.val().tUsername) && (loggedPword == childData.val().tPassword) ){
				currentKey = childData.key;
			}
		});
		keyRef.update({currentTailor: currentKey});
		alert("key = " + keyRef.currentTailor);
	});
	
}

the .update method was successful since it stored the corresponding key for the logged in user:

the updated database

but when I try to retrieve it using alert, it says UNDEFINED! I don't understand because I'm assuming that I'm calling from the root reference of the database, that's as low as I go.

I tried making a seperate .on() function for the keyRef, but it's still the same result.

keyRef.on('value', function(data){
  alert(data.currentTailor);
});

Did I called the data in a wrong way?

解决方案

The correct way would be:

keyRef.on('value', function(data){
 //  alert(data.val().currentTailor); see the difference 
     console.log('currentTailor ==>', data.val().currentTailor);
});

alert() in javascript is not recommended as it freezes your page. Use console instead. This way you can see your logs in browser's console (in chrome press Ctrl+Shift+J)

Firebase's on() method returns the callback function . To get the data object, you have to use val() method on that return value. Then you can get any value using corresponding key.

 keyRef.on('value', function(data){
// here data is callback function
});

keyRef.on('value', function(data){
//data.val() returns the data in object form
});
 keyRef.on('value', function(data){
 // data.val().currentTailor to access currentTailor attribute
 });

You can console each of them to see what is the return values.

这篇关于Firebase数据即使包含内容,也会返回UNDEFINED值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆