2014-09-13 44 views
0

我有一个父对象“列表”(想想房地产列表),可以有多个子对象“图像”。解析云代码 - 查询指向父代的子代

我试图实现一个云代码函数,它将所有子对象标记为归档时,我归档他们的父母。

由于某些原因,查询结果始终为空。我看不出为什么。我的“错误:图像未定义”错误每次都会出现。

Image类有一个指向Listing的指针,但是Listing和Image没有关系。

Parse.Cloud.afterSave("Listing", function(request) { 

    Parse.Cloud.useMasterKey(); 

    // Handle archiving. If a listing is marked as archived, mark the image as archived also. 

    query = new Parse.Query("Image"); 
    query.equalTo("listing", request.object); 

    query.first({ 
     success: function(image) { 

      if (typeof image != "undefined") { 
       image.archived(request.object.get("archived")); 
       image.save(); 

       console.log("Done"); 
      } 
      else { 
       console.log("Error: image undefined."); 
      } 
     }, 
     error: function(error) { 
      console.error("Got an error " + error.code + " : " + error.message); 
     }, 
    }); 
); 

任何帮助表示赞赏。

回答

0

我已经能够得到这个使用工作如下:

Parse.Cloud.afterSave("Listing", function(request) { 

    Parse.Cloud.useMasterKey(); 

    // Handle archiving. If a listing is marked as archived, mark the image as archived also. 

    query = new Parse.Query("Image"); 
    query.equalTo("listing", request.object); 

    query.find({ 
     success: function(images) { 

      if (typeof images != "undefined") { 

       for (i = 0 ; i < images.length; i++) { 

        var theImage = images[i]; 
        theImage.set("archived", request.object.get("archived")); 
        theImage.save(); 
       } 

       console.log("Done"); 
      } 
      else { 
       console.log("Error: image undefined."); 
      } 
     }, 
     error: function(error) { 
      console.error("Got an error " + error.code + " : " + error.message); 
     }, 
    }); 
);