无法创建Parse对象

时间:2015-06-11 06:18:05

标签: parse-platform

这是我创建的后台工作,用于从Google表格中获取JSON,解析它,创建Parse对象,然后将它们保存到我的Parse Core中。

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

    // Use master key
    Parse.Cloud.useMasterKey();

    // ID of the Google Spreadsheet
    var spreadsheetID = "someId";
    var sheetID = "anotherId";

    var url = "https://spreadsheets.google.com/feeds/list/" + spreadsheetID + "/" + sheetID + "/public/values?alt=json"; 



    // Cloud function
    Parse.Cloud.httpRequest({
      url: url
    }).then(function(httpResponse) {

      // Parse response
      var json = JSON.parse(httpResponse.buffer);

      // Array to store new objects
      var playlistsToSave = [];

      // Create playlist objects
      for (var i = 0; i < json.feed.entry.length; i++) {

        var each = json.feed.entry[i];

        var name = each["gsx$name"]["$t"];
        var playlistid = each["gsx$playlistid"]["$t"];
        var description = each["gsx$description"]["$t"];
        var imageurl = each["gsx$imageurl"]["$t"];
        var categories = each["gsx$categories"]["$t"].split(','); 


        // -- Logs
        console.log('name = ' + name);
        console.log('playlistid = ' + playlistid);
        console.log('description = ' + description);
        console.log('imageurl = ' + imageurl);


        // Create Parse objects 
        var Playlist = Parse.Object.extend("Playlist");
        var playlist = new Playlist();
        playlist.set("name", name);
        playlist.set("playlistid", playlistid);
        playlist.set("description", description);
        playlist.set("imageurl", imageurl);
        playlist.set("categories", categories);

        // -- Logs
        console.log(playlist.playlistid);

        // Add to caching array
        playlistsToSave.push(playlist);



      }


console.log('playlistsToSave = ');
console.log(playlistsToSave);


      // Parse - Save objects
      Parse.Object.saveAll(playlistsToSave, {
        success: function(saveList) {
            status.success("Objects created successfully.");
        },
        error: function(error) {
            status.error("Unable to save objects.");
        }
      });


    },function(httpResponse) {
      // error
      console.error('Request failed with response code ' + httpResponse.status);

      status.error("Scheduled messages error: " + error);
    });

});

JSON正好回归。问题是,当我尝试创建新的Parse对象时,似乎没有创建它们。

退出:

    console.log(playlist.playlistid);

返回:

I2015-06-11T06:11:21.235Z]No Message provided

另外,记录:

 console.log('playlistsToSave = ');
 console.log(playlistsToSave);

返回:

I2015-06-11T06:11:21.236Z]playlistsToSave = 

1 个答案:

答案 0 :(得分:0)

我根据Luca的答案更改了日志记录:

console.log(playlist.get("playlistid"));

会打印出一个值,因此会创建播放列表对象并设置值。还有:

console.log(playlistsToSave.length);

显示值2

有趣的是,当我这样做并且播放列表对象保存到Parse时,我收到了成功消息。

显然这一行:

console.log(playlistsToSave);

导致云日志中的消息出错:

Failed with: Uncaught Tried to save an object with a pointer to a new, unsaved object.

为什么呢?我不知道。所以我的调试日志记录错误开始并抛弃我,但尝试注销该数组也导致异常。我来自一个Objective-c背景,所以注销一个像这样的数组会打印数组对象。