2016-01-22 106 views
1

我有一个包含多个指针的类,我想在响应中返回其完整对象(如SQL中的联接)。下面的例子是这样做的,但它返回JSON而不是Parse对象。如何从指针中返回包含每个Parse对象的对象数组,而不对每个指针进行额外的查询?解析 - JavaScript指针查询,从指针返回解析对象

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 
       } 
      }); 
     } 
    }) 

编辑(我与雨燕建立在客户端上):

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", 
    } 
}] 

我更喜欢前者。

+0

你可以添加,设置了查询代码,并且可以显示结果以某种方式(如通过日志输出),并解释它们不是你所期望的?获得“JSON而不是对象”的想法对我来说很有趣。 JSON是已经序列化的对象的字符串表示形式。这些是否解析对象取决于JSON所说的内容。 – danh

+0

另一种说法是:如果'var postName = obj.get(“post”)。get(“postName”);''在'postName'中给你一个不错的字符串,而不是错误,那么你的代码就可以工作。你有post对象,不需要另一次获取。 – danh

+0

@danh - 查看我的更新。 – diskodave

回答

0

调用者正在获取部分对象(只是某些属性),因为云功能使用它构造的一些属性进行响应。为了得到解析的对象回来,返回解析对象...

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)") 
    } 
} 
+0

嘿,[检查此问题](http://chat.stackoverflow.com/rooms/100720 /加入最爱特征) –