2016-01-22 162 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 /加入最愛特徵) –