【发布时间】:2016-06-02 07:57:35
【问题描述】:
我创建了一个画布游戏并将其放置在网站中。当玩家获得高分时,我想将新的高分发布到我创建的数据库中。我的游戏的部分脚本(我要发送到高分数据库的位)如下:
var scoreArray = [];
$.get('api/listScores.php', function(myData){
$.each(myData, function(name, val){
console.info(name);
console.info(val);
var tempHTML = "";
//loop through the data
$.each(myData, function(name, val){
//store a reference to the score
scoreArray.push(val);
tempHTML +='<li>';
tempHTML +='<span>';
tempHTML += val.dbScore;
tempHTML +='</span>'
tempHTML +='<span>'
tempHTML += val.dbPlayer;
tempHTML +='</span>'
tempHTML +='</li>'
});
$('.highScores').html(tempHTML);
});
// scoreArray now has a value
var lastItem = scoreArray.pop();
//check if function scope 'score' or global scope 'score' is greater than the object 'lastItem.dbScore'.
if (score > lastItem.dbScore)
{
$.post('api/addNewHighScore.php',
{
gamePlayer: playerName,
gameScore: score
}
);
}
});
在“api/listScore.php”中,我按降序列出了我的数据库的行。
我目前遇到的错误是:
未捕获的类型错误:无法读取未定义的属性“dbScore”
此错误指的是以下行:
if (score > lastItem.dbScore)
这是否意味着 scoreArray.pop() 不起作用? 我应该以不同的方式解决这个问题吗?
抱歉,不是所有的代码都在这里,有很多源代码文件和代码行我不能放在一篇文章中。
【问题讨论】:
-
您是否将 lastItem 登录到控制台并检查了它所持有的内容?
标签: javascript php ajax database