2014-09-13 43 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); 
     }, 
    }); 
);