从Parse.Query.get文档返回的gameScore Parse.Object对象提供了未定义的gameScore对象 [英] Returned gameScore Parse.Object object from Parse.Query.get documentation gives undefined gameScore object

查看:65
本文介绍了从Parse.Query.get文档返回的gameScore Parse.Object对象提供了未定义的gameScore对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直接来自 Parse.com JavaScript指南:

var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
query.get("xWMyZ4YEGZ", {
  success: function(gameScore) {
    // The object was retrieved successfully.
  },
  error: function(object, error) {
    // The object was not retrieved successfully.
    // error is a Parse.Error with an error code and message.
  }
});

var score = gameScore.get("score");
var playerName = gameScore.get("playerName");
var cheatMode = gameScore.get("cheatMode");

基本上给出了Uncaught ReferenceError:未定义gameScore ...

Basically gives Uncaught ReferenceError: gameScore is not defined...

这是我的代码来解决此问题:

Here is my code to troubleshoot this:

// Retrieve class
var GameScore = Parse.Object.extend("GameScore"); //redefine the GameScore class based on the cloud.
var query = new Parse.Query(GameScore);
var my_object;
var some_object = query.get("SQV1DZXv5p", {
    success: function(gameScore) { //gameScore is the retrieved object.
        alert('Object retrieved, with object ID: ' + gameScore.id);
        //document.getElementById('division1').innerHTML = gameScore.id; 
        my_object = gameScore.id;

    }, 
    error: function(object, error) {
        alert('Retrieval FAILURE, error: ' + error.message + '; Object retrieved instead is: ' + object); //object will be NULL if not found.
    }
});

var score = gameScore;
document.getElementById('division1').innerHTML = my_object;

因此,这仍然会引发gameScore的参考错误. 另外,document.getELementById语句不会在我的division1 div中打印gameScore.id.它仍然是空的.但是,当我检查JavaScript控制台中的my_object是什么时,它会正确返回gameScore.id.

So this still throws the reference error for gameScore. Also, the document.getELementById statement does not print the gameScore.id in my division1 div. It remains empty. But when I check to see what my_object is in the javascript console, it returns the gameScore.id correctly.

有趣的是,如果我在成功函数中运行document.getELementById ,那么它将显示gameScore.id ...

What's interesting is if I run the document.getELementById line inside the success function, then it will display the gameScore.id...

推荐答案

执行以下操作:

var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
query.get("xWMyZ4YEGZ", {
  success: function(gameScore) {
    // The object was retrieved successfully.
    var score = gameScore.get("score");
    var playerName = gameScore.get("playerName");
    var cheatMode = gameScore.get("cheatMode");
  },
  error: function(object, error) {
    // The object was not retrieved successfully.
    // error is a Parse.Error with an error code and message.
  }
});

gameScore仅在成功块的范围内可用,因此是Uncaught ReferenceError.成功障碍之外的世界并不知道它的存在.在那做你的东西.

gameScore is only be available in the scope of the success block, hence the Uncaught ReferenceError. The world outside the success block does not know it exists. Do your stuff in there.

此外,.get()从服务器发出请求,因此需要花费一些时间才能完成.在.get()方法之后执行gameScore操作将导致在尚未获取该对象时访问该对象,因此会出现另一个错误.请参阅下面的@danh评论.对于这样的网络调用,请始终对完成块中获取的对象执行操作.

Also, .get() makes a request from the server, so it would take time to complete. Performing operations gameScore after the .get() method will result in accessing that object when it has not been fetched yet, hence another error. See @danh's comment below. For network calls like this, always perform actions on the fetched object inside the completion block.

这篇关于从Parse.Query.get文档返回的gameScore Parse.Object对象提供了未定义的gameScore对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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