从Parse.Query.get文档返回gameScore Parse.Object对象给出了不确定

2019-09-28 06:45发布

直接从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");

基本上给出了未捕获的ReferenceError:gameScore没有定义...

这里是我的代码来解决此:

// 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语句不打印gameScore.iddivision1股利。 它仍然是空的。 但是,当我检查,看看有什么my_object是在JavaScript控制台,它返回gameScore.id正确。

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

Answer 1:

做这个:

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仅在成功块,因此未捕获的ReferenceError的范围可用。 成功的块之外的世界并不知道它的存在。 不要在有你的东西。

此外, .get()让来自服务器的请求,所以这需要时间来完成。 执行操作gameScore.get()方法将导致访问该对象时,它没有被取出的是,因此其他错误。 见下面@ DANH的评论。 对于网络电话这样的,总是完成块内所取得的对象执行的操作。



文章来源: Returned gameScore Parse.Object object from Parse.Query.get documentation gives undefined gameScore object