- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的网页中,我尝试从一个 Firebase 数据库进行查询并在另一个 Firebase 数据库上进行更新。
问题是当我尝试对第二个数据库进行更新时,我收到一条错误消息,指出该属性未定义。
我已经放置了几个控制台日志,并且我确信它们包含一个数值,所以我不确定为什么 firebase 提示该属性未定义。
我的 HTML 页面
<html>
<head>
<meta charset="utf-8">
<title>
Firebase Test
</title>
</head>
<style>
.alright
{
text-align: left;
border: 1px solid black;
border-collapse: collapse;
}
</style>
<body>
<input type="button" value="SHUFFLE" onClick="window.location.reload()">
<table class="alright">
<tr>
<th><h1>Niyant</h1></th>
<th><h1>Varun</h1></th>
</tr>
<!--<tr>
<th><h1 id="niyantscoretag"></h1></th>
<th><h1 id="varunscoretag"></h1></th>
</tr>-->
<tr>
<td><h2 id="ns">0</td>
<td><h2 id="vs">0</td>
</tr>
<tr>
<td><h3 id="nrank">0</td>
<td><h3 id="vrank">0</td>
</tr>
<tr>
<td><img id="imgtag1"/></td>
<td><img id="imgtag2"/></td>
</tr>
<tr>
<td><h2>Winner is</h2></td>
<td><img id="imgtag3"/></td>
</tr>
</table>
<script src="https://www.gstatic.com/firebasejs/3.9.0/firebase.js"></script>
<script>
var config = {
apiKey: "AIzaSyAXpEHm1uLfRLS-6QCun1iVioquEmpmbAI",
authDomain: "helloworld-3c2e7.firebaseapp.com",
databaseURL: "https://helloworld-3c2e7.firebaseio.com",
projectId: "helloworld-3c2e7",
storageBucket: "helloworld-3c2e7.appspot.com",
messagingSenderId: "103101977379"
};
firebase.initializeApp(config);
var otherAppConfig = {
apiKey: "AIzaSyC3UBiLaBJoW0vfY5xJgtFWV_zdKl9vfWo",
authDomain: "scoringdev-b5ede.firebaseapp.com",
databaseURL: "https://scoringdev-b5ede.firebaseio.com",
projectId: "scoringdev-b5ede",
storageBucket: "scoringdev-b5ede.appspot.com",
messagingSenderId: "123331117443"
};
var database1 = firebase.initializeApp(config,"primary");
var database1_database = database1.database();
var database2 = firebase.initializeApp(otherAppConfig, "secondary");
var database2_database = database2.database();
/************HERE**********/
var nscoref = database2_database.ref('Niyant/Score');
var vscoref = database2_database.ref('Varun/Score');
var vscore,nscore,vscoreincremented,nscoreincremented;
nscoref.on('value',
function(snapshot){
nscore = snapshot.val();
nscoreincremented = nscore + 1;
console.log("nscore = "+nscore);
console.log("nscoreincremented = "+nscoreincremented);
}
);
vscoref.on('value',
function(snapshot){
vscore = snapshot.val();
vscoreincremented = vscore + 1;
console.log("vscore = "+vscore);
console.log("vscoreincremented = "+vscoreincremented);
}
);
var URLPath1,URLPath2,nsv,vsv;
var num1 = Math.floor(Math.random() * 100) + 1;
var num2 = Math.floor(Math.random() * 100) + 1;
if(num1 < num2)
{
database2_database.ref('Niyant').set({Score: nscoreincremented});
}
else if(num1 > num2)
{
database2_database.ref('Varun').set({Score: vscoreincremented});
}
var imgref1 = database1_database.ref(num1+'/Location');
var imgref2 = database1_database.ref(num2+'/Location');
var imgref1_name = database1_database.ref(num1+'/Name');
var imgref2_name = database1_database.ref(num2+'/Name');
imgref1.on('value',
function(snapshot){
URLPath1 = snapshot.val();
console.log(URLPath1);
console.log(snapshot.val());
document.getElementById("imgtag1").src = URLPath1;
document.getElementById("nrank").innerText = 'Rank '+num1;
if(num1 < num2)
{
document.getElementById("imgtag3").src = URLPath1;
}
}
);
imgref2.on('value',
function(snapshot){
URLPath2 = snapshot.val();
console.log(URLPath2);
document.getElementById("imgtag2").src = URLPath2;
document.getElementById("vrank").innerText = 'Rank '+num2;
if(num2 < num1)
{
document.getElementById("imgtag3").src = URLPath2;
}
}
);
imgref1_name.on('value',
function(snapshot){
URLPath1_name = snapshot.val();
console.log(URLPath1_name);
console.log(snapshot.val());
document.getElementById("ns").innerText = URLPath1_name;
}
);
imgref2_name.on('value',
function(snapshot){
URLPath2_name = snapshot.val();
console.log(URLPath2_name);
document.getElementById("vs").innerText = URLPath2_name;
}
);
</script>
</body>
</html>
这是来自 Chrome 控制台。
有问题的更新在源代码中用“***HERE****”注释掉以供引用。
有人可以帮我吗?
最佳答案
数据是从 Firebase 异步读取的。您的代码无法满足这一要求,这会导致您在实际加载之前尝试设置
分数。
解决方案是正确同步对数据的访问:首先确保所有内容均已加载,然后才更新分数。
var nscoref = database2_database.ref('Niyant/Score');
var vscoref = database2_database.ref('Varun/Score');
var vscore,nscore,vscoreincremented,nscoreincremented;
function updateScore() {
var num1 = Math.floor(Math.random() * 100) + 1;
var num2 = Math.floor(Math.random() * 100) + 1;
if(num1 < num2) {
database2_database.ref('Niyant').set({Score: nscoreincremented});
}
else if(num1 > num2) {
database2_database.ref('Varun').set({Score: vscoreincremented});
}
}
nscoref.on('value', function(snapshot){
nscore = snapshot.val();
nscoreincremented = nscore + 1;
if (nscoreincremented && vscoreincremented) {
updateScore();
}
});
vscoref.on('value', function(snapshot){
vscore = snapshot.val();
vscoreincremented = vscore + 1;
if (nscoreincremented && vscoreincremented) {
updateScore();
}
});
关于javascript - 火力地堡 : First Argument contains undefined in property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44013321/
目前,我正在为网络开发类(class)做作业。 这些是说明:第一行和首字下沉样式Jakob 希望文章的第一行以小写大写字母显示。转到 First Line and Drop Cap Styles 部分
.first() 方法是在 jQuery 1.4 中添加的。 :first 选择器自 1.0 以来就已存在。 来自文档: :first The :first pseudo-class is equiv
我正在审查现有的 ASP.NET MVC (5.2.3) EF (6.1.3) 项目。 该项目使用 ASP.NET Identity,我检查了 web.config 中的 2 个连接字符串,一个用于
为什么人们使用 mid=first+(last-first)/2 而不是 (first+last)/2,在二进制搜索的情况下)两者有区别吗。如果有,请告诉我,因为我无法理解其中的区别。 最佳答案 如果
为什么人们使用 mid=first+(last-first)/2 而不是 (first+last)/2,在二进制搜索的情况下)两者有区别吗。如果有,请告诉我,因为我无法理解其中的区别。 最佳答案 如果
for(auto it = M.begin(); it!=M.end();it++) { coutfirstsecondsecond == 1) return it->firs
我试图从第二个循环中获取循环的第一项。 我知道我得到了这样的@key @../key 但@first 似乎不像@../first 那样工作 有什么想法吗? 问候 最佳答案 首先,无论是否在嵌套 blo
var tab1 = $('.tabs a:first-child').attr('href'); alert(tab1); .. 尽管同一页面上有两个 div.switch,但仅匹配一个。第二个位于
我想知道如何将节点*变量 NODE 分配给结构内的数据? struct node { int info; struct node *link; }; typedef struct nod
我有两个段落包含在一个 div 中。我想让第一段的文字变大一点,但使用 :first-child 并不能像我所说的那样工作。看不出有什么问题。
我有一个 ul li 列表 Parent child1 child2
我有三个表,即员工、部门和申诉。 Employees 表有超过一百万条记录。我需要找到员工的详细信息、他/她的部门以及他/她提出的申诉。 我可以想到以下两个查询来查找结果: 1。先过滤记录,只获取需要
我有三个表,即员工、部门和申诉。 Employees 表有超过一百万条记录。我需要找到员工的详细信息、他/她的部门以及他/她提出的申诉。 我可以想到以下两个查询来查找结果: 1。先过滤记录,只获取需要
这有什么区别吗: myList.Where(item => item == 0).First(); 还有这个: myList.First(item => item == 0); 后者对我来说更有意义,
我分不清 element:first-child 之间的区别和 element:first-of-type 例如,你有一个 div div:first-child → 全部 元素是其父元素的第一个子元
当我遇到一个奇怪的情况时,我正在研究 CSS 选择器。 如果我使用 :first-child 伪元素,我需要在它前面加上一个空格才能工作,否则它将无法工作。然而 :first-letter 伪元素的情
请考虑以下字符串数组: let strings = ["str1", "str2", "str10", "str20"] 假设需要获取包含 5 个字符的第一个元素 (String),我可以使用 fil
让我们假设我们要开始新项目 - 包含一些业务逻辑的应用程序、ASP.NET 上的用户界面、WPF 或两者。我们想使用 ORM 或 DAL 代码生成器并在 .NET 类中实现我们的业务逻辑。我们可以通过
我有一种树系统。我想做的是给所有 parent 一个 margin ,除了第一个。这是我的 HTML: Test
我分不清 element:first-child 之间的区别和 element:first-of-type 例如,你有一个 div div:first-child → 全部 元素是其父元素的第一个子元
我是一名优秀的程序员,十分优秀!