继续收到错误:&#34;失败:未捕获的SyntaxError:<unknown file =“”>中出现意外的令牌T:1&#34; (PARSE CLOUD CODE)</unknown>

时间:2014-06-30 15:31:07

标签: javascript json parse-platform

如果有人可以帮助我,那将是一个救生员!!

(我正在使用PARSE)

基本上这个解析工作试图做的是 1)查询名为channel的类的所有对象 2)循环遍历从查询返回的“Results”数组中的每个对象 3)拨打一个返回JSON字符串的Google API 4)解析JSON并保存名为Videos

的对象的新实例

问题是我不断收到错误: 失败:未捕获的SyntaxError:意外的令牌T in:1 失败:未捕获的SyntaxError:输入的意外结束:0

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

var query = new Parse.Query("Channel");
query.find ({
    success: function (results) {


        var httpRaw;

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

            var channel_id = results[i].get("channel_id");

            Parse.Cloud.httpRequest({
                url: 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCKy1dAqELo0zrOtPkf0eTMw&maxResults=50&order=viewCount&type=video&key=AIzaSyCLGCJOPU8VVj7daoh5HwXZASnmGoc4ylo',
                success: function (httpResponse) {

                    httpRaw = httpResponse.text;

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

                }

            });


            var json = JSON.parse(httpRaw);

            for (var z = 0; z < json.items.length ; z++){

                var video = new Parse.Object("Video");
                video.set("video_id", json.items[z].id.videoId.toString());
                video.set("video_title", json.items[z].snippet.title.toString());
                video.set("video_description", json.items[z].snippet.description.toString());
                video.set("video_thumbnail", json.items[z].snippet.thumbnails.medium.url.toString());
                video.set("date_published", json.items[z].snippet.publishedAt.toString());

                var relation = video.relation("parent_channel");
                relation.add(results[i]);
                video.save();
            }


        }

    },
    error: function() {

    }
});

});

1 个答案:

答案 0 :(得分:4)

我猜测原因是JSON.parse()。 HTTP请求在云代码中是非阻塞的(通常在JavaScript中无处不在),因此在JSON.parse()设置之前评估httpRaw

至少,您需要将parse()调用和以下循环移动到HTTP请求的成功处理程序中,以便它们等到您有一个有效的响应。我建议使用Promise而不是成功/错误回调。

以下是我将如何处理它(警告:未经测试的代码如下......)

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

  var query = new Parse.Query("Channel");
  query.find().then(function(results) {
    var requests = [];

    for (var i = 0; i < results.length; i++) {
      var channel_id = results[i].get("channel_id");

      requests.push(Parse.Cloud.httpRequest({
        url: 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCKy1dAqELo0zrOtPkf0eTMw&maxResults=50&order=viewCount&type=video&key=AIzaSyCLGCJOPU8VVj7daoh5HwXZASnmGoc4ylo'
      }));
    }

    return Parse.Promise.when(requests);
  }).then(function(results) {
    var videos = [];

    for(var i = 0; i < results.length; i++) {
      var httpRaw = results[i].text;
      var json = JSON.parse(httpRaw);

      for (var z = 0; z < json.items.length ; z++){

        var video = new Parse.Object("Video");
        video.set("video_id", json.items[z].id.videoId.toString());
        video.set("video_title", json.items[z].snippet.title.toString());
        video.set("video_description", json.items[z].snippet.description.toString());
        video.set("video_thumbnail", json.items[z].snippet.thumbnails.medium.url.toString());
        video.set("date_published", json.items[z].snippet.publishedAt.toString());

        var relation = video.relation("parent_channel");
        relation.add(results[i]);

        videos.push(video);
      }
    }

    return Parse.Object.saveAll(videos);
  });

});