【问题标题】:Parse - JavaScript Pointer Query, issue returning Parse Objects from pointersParse - JavaScript 指针查询,从指针返回解析对象的问题
【发布时间】:2016-01-22 23:01:37
【问题描述】:

我有一个带有多个指针的类,我想在响应中返回它们的完整对象(如 SQL 中的连接)。下面的示例执行此操作,但它返回 JSON 而不是 Parse 对象。如何在不对每个指针进行额外查询的情况下从指针返回包含每个 Parse 对象的对象数组?

    query.find().then(function(results){
        /* Go Through Each Comment*/
        var commentsArray = new Array();
        for(i in results){
            /* Set obj to current comment*/
            var obj = results[i];
            /* Get Post's Name */
            var postName = obj.get("post").get("postName");
            /* Get Post's Message */
            var postMsg = obj.get("post").get("postMsg");
            /* Get Post Author's Name */
            var authorName = obj.get("post").get("postAuthor").get("name");
            /* Get Comment's Message */
            var commentMsg = obj.get("msg");
            /* Get Comment's Author*/
            var commentAuthor = obj.get("user").get("name");

            /* Let's Put the Comment Information in an Array as an Object*/
            commentsArray.push({
                post:{
                    name: postName,
                    msg: postMsg
                },
                author: authorName,
                comment: {
                    author: commentAuthor,
                    msg: commentMsg
                }
            });
        }
    })

已编辑(我正在客户端上使用 Swift 构建):

var query = new Parse.Query("Profile");

query.equalTo("objectId", objectId);

query.find().then(function(profile) {
  response.success(profile)  // Returns profile parse object
}, function(error) {
  response.error(error);
});


//RETURNS
"<Profile: 0x153f96570, objectId: HKdukNBlmA, localId: (null)> {\n}"



Parse.Cloud.define("getProfiles", function(request, response) {
  var query = new Parse.Query("Profile");

  query.include("friendId");

  query.find().then(function(profiles) {
    var res = [];

    profiles.forEach(function(profile) {
      var obj = {
        firstName: profile.get("firstName"),
        lastName: profile.get("lastName"),
        age: profile.get("age"),
        gender: profile.get("gender")
      };

      obj.friend = {
        firstName: profile.get("friendId").get("firstName"),
        lastName: profile.get("friendId").get("lastName"),
      };

      res.push(obj);
    });

    response.success(res);
  }, function(error) {
    response.error("Error: " + error.code + " " + error.message);
  });
});


// RETURNS
[{
  firstName: "bob",
  lastName: "smith",
  age: 19,
  gender: male
  friend: {
    firstName: "tim",
    lastName: "wang",
  }
},
{
  firstName: "chris",
  lastName: "scalia",
  age: 24,
  gender: male
  friend: {
    firstName: "ben",
    lastName: "calder",
  }
}]

我更喜欢前者。

【问题讨论】:

  • 您能否添加设置查询的代码,您能否以某种方式显示结果(例如通过日志输出)并解释它们为何与您的预期不符?获取“JSON 而不是对象”的想法对我来说很有趣。 JSON 是已序列化对象的字符串表示形式。这些是否是解析对象取决于 JSON 所说的内容。
  • 另一种说法:如果var postName = obj.get("post").get("postName");postName 中为您提供了一个不错的字符串,并且没有错误,那么您的代码就可以工作。你有 post 对象,不需要另一个 fetch。
  • @danh - 查看我的更新。
  • @danh 我更喜欢将 Parse 对象数组返回给客户端(iOS - Swift)。例如:(" {\n}", " {\n}", " {\n}" )

标签: javascript pointers parse-platform


【解决方案1】:

调用者正在获取部分对象(只是一些属性),因为云函数使用它构建的少数属性进行响应。要取回解析对象,请返回解析对象...

Parse.Cloud.define("getProfiles", function(request, response) {
    var query = new Parse.Query("Profile");
    query.include("friendId");
    query.find().then(function(profiles) {
      response.success(profiles);
    }, function(error) {
      response.error("Error: " + error.code + " " + error.message);
    });
});

当然,这不需要是云函数,因为它不会为查询添加任何值。客户端可以只运行查询。

另外请注意,我看到的另一件事,尤其是 swift 作者调用 parse 时,是一种奇怪的习惯,即进行查询,获取 PFObjects 数组作为响应,然后迭代这些对象并仅存储一些属性在本地数组中。这通常是一个错误,所以在我看来,你的客户应该是这样的......

// no need to call a cloud function that just does a query
var query = PFQuery(className:"Profile")
query.includeKey("friendId")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
    if error == nil {
        // don't iterate the objects here, saving bits of them in an array
        // have an instance var that is an array of PFObjects
        self.objects = objects;
    } else {
        print("Error: \(error!) \(error!.userInfo)")
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多