2016-01-24 114 views
0

幾年前,我爲一個Parse.com移動應用程序編寫了一些後端代碼,並且剛剛被要求添加一個功能。但是,我發現經過小小的調整後,代碼就不會成功。所以,我回到工作副本,下載,然後部署回來,它也不會工作!我想知道這是否是Parse軟件的變化?解析雲代碼保存問題

代碼在保存方法失敗,因爲所有的日誌都很好。錯誤情況的日誌顯示'沒有提供信息'。如果我不使用消息屬性,它只會顯示「{}」,所以我認爲它是空的。在錯誤情況下,我已將承諾解決方案放在調試時停止作業超時。我從來沒有理解的一件事是爲什麼我必須製作兩個Seed對象,然後從一個對象上正確保存。如果我做了一個.save(null,...)它將無法工作。

任何幫助將是太棒了。謝謝!

PS:對於下面的縮進道歉 - 在我的文件中是正確的。

function flush() { 
    //Clear the previous records from the class. 
    var Seed = Parse.Object.extend("Seeds"); 
    var _ = require("underscore"); 
    var arr = []; 
    var query = new Parse.Query(Seed); 
    return query.find().then(function(oldSeeds) { 
    _.each(oldSeeds, function(oldSeed) { 
     arr.push(oldSeed.destroy()); 
    }); 
    return Parse.Promise.when(arr); 
    }); 
} 

Parse.Cloud.job("fetchjson", function(request, status) { 

    var url = 'someurl'; 

    flush().then(function() { Parse.Cloud.httpRequest({url: url}).then(function(httpResponse){ 
    var Seed = Parse.Object.extend("Seeds"); 
    var jsonobj = JSON.parse(httpResponse.text); 
    var _ = require("underscore"); 

    var results = []; 
    // do NOT iterate arrays with `for... in loops` 
    _.each(jsonobj.seeds, function(s) { 

     var p = new Parse.Promise(); 
     results.push(p); // Needs to be done here or when() will execute immediately with no promises. 
     var seed = new Seed(); 
     var a = new Seed(s); 
     var image_url = a.get("image") 

     //Get the JSON. 
     Parse.Cloud.httpRequest({url: image_url}).then(function(response) { 
      console.log("Fetching image at URL: " + image_url); 
      //Create a new image object and save, passing ref through promise. 
      var file = new Parse.File('thumb.jpg', { base64: response.buffer.toString('base64', 0, response.buffer.length) }); 
      return file.save(); 
     }).then(function(thumb) { 
      console.log("Attaching thumb to object"); 
      //Set image ref as object attribute. 
      a.set("imageFile", thumb); 
      console.log("Parsing views into viewsint"); 
      //Save decimal string as int into another attribute. 
      a.set("viewsInt", parseInt(a.get("views"))); 
     console.log("Parsing description into descriptionarray"); 
      //Save string as array into another attribute. 
     var dar = new Array(1); 
     //dar[0] = a.get("description") 
      a.set("descriptionarray", [a.get("description")]); 
     }, function(error) { 
      console.log("Error occurred :("); 
     }).then(function(){ 
      console.log("Saving object"); 
      //Save the object and resolve the promise so we can stop. 
      seed.save(a,{ 
      success: function(successData){ 
       console.log(successData); 
       p.resolve(successData); 
      }, 
      error: function(error){ 
       console.log(error.message); 
       p.resolve(error); 
      } 
      }); 
     }); 
    }); 
    // .when waits for all promises to be resolved. This is async baby! 
    Parse.Promise.when(results).then(function(data){ 
     console.log("All objects saved"); 
     status.success("Updated Succesfully"); 
    }); 
    }, function(error) { 
    //Oh noes :'(
    console.error('Request failed with response code ' + httpResponse.status); 
    status.error("Update Failed"); 
    }); 
}); 
}); 

回答

0

我改變你的代碼了一下,把一些註釋來解釋:

// DEFINE THESE ON THE TOP. NO NEED TO REPEAT. 
var _ = require("underscore"); 
var Seed = Parse.Object.extend("Seeds"); 

function flush() { 
    //Clear the previous records from the class. 
    var arr = []; 
    var query = new Parse.Query(Seed); 
    return query.find().then(function(oldSeeds) { 
    _.each(oldSeeds, function(oldSeed) { 
     arr.push(oldSeed.destroy()); 
    }); 
    return Parse.Promise.when(arr); 
    }); 
} 

Parse.Cloud.job("fetchjson", function(request, status) { 

    var url = 'someurl'; 

    flush().then(function() { 
    Parse.Cloud.httpRequest({url: url}).then(function(httpResponse){ 
     var jsonobj = JSON.parse(httpResponse.text); 

     var results = []; 

     _.each(jsonobj.seeds, function(s) { 

     // ONE SEED OBJECT WITH INITIAL SET OF DATA FROM JSON 
     var seed = new Seed(s); 

     var image_url = seed.get("image") 

     // A SERIAL PROMISE FOR EACH SEED 
     var promise = Parse.Cloud.httpRequest({url: image_url}).then(function(response) { 
      console.log("Fetching image at URL: " + image_url); 
      //Create a new image object and save, passing ref through promise. 
      var file = new Parse.File('thumb.jpg', { base64: response.buffer.toString('base64', 0, response.buffer.length) }); 
      return file.save(); 
     }).then(function(thumb) { 
      // SETTING MORE PROPERTIES 
      //Set image ref as object attribute. 
      console.log("Attaching thumb to object"); 
      seed.set("imageFile", thumb); 

      //Save decimal string as int into another attribute. 
      console.log("Parsing views into viewsint"); 
      seed.set("viewsInt", parseInt(seed.get("views"))); 

      //Save string as array into another attribute. 
      console.log("Parsing description into descriptionarray"); 
      seed.set("descriptionarray", [seed.get("description")]); 

      // SAVING THE OBJECT 
      console.log("Saving object"); 
      return seed.save(); 
     }); 

     // PUSH THIS PROMISE TO THE ARRAY TO PERFORM IN PARALLEL 
     results.push(promise); 
     }); 

     Parse.Promise.when(results).then(function(data){ 
     console.log("All objects saved"); 
     status.success("Updated Succesfully"); 
     }); 
    }, function(error) { 
     console.error('Request failed with response code ' + httpResponse.status); 
     status.error("Update Failed"); 
    }); 
}); 
}); 
0

感謝knshn。自從該版本(包括您所做的一些更改)之後,我已經對代碼進行了重構,但是我發佈了與以前工作正常的版本相同的版本。您的更改讓我看到正確的錯誤。出於某種原因,簡單的單個對象實現原來並不適用於我,因此是一個討厭的解決方法。它現在可以工作了。

我已經找到了罪魁禍首 - 種子類有一個屬性,稱爲'ID'。用舊版本,這工作得很好,但是當我現在部署該代碼時,它給出了錯誤101:'找不到更新的對象'。這一定是因爲新的Parse代碼將其與內部objectId混合在一起,並且感到困惑,因爲id與預期的不同。我想知道如何仍然可以與回滾工作。也許在版本被標記爲使用較舊的分析代碼。

我的修復是爲id使用不同的名稱 - 'seed_id'。