user3711987
user3711987

Reputation: 303

Keep getting error: "Failed with: Uncaught SyntaxError: Unexpected token T in <unknown file>:1" (PARSE CLOUD CODE)

If someone could help me with this it would be a life saver!!

(I'm using PARSE)

Basically what this parse job attempts to do is 1) queries all objects of a class called channel 2) loop through each object in the "Results" array which is returned from the query 3) make a call to a Google API which returns a JSON string 4) parse the JSON and save new instances of an Object called Videos

The problem is i keep getting the errors: Failed with: Uncaught SyntaxError: Unexpected token T in :1 Failed with: Uncaught SyntaxError: Unexpected end of input in :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() {

    }
});

});

Upvotes: 0

Views: 2477

Answers (1)

ryanjduffy
ryanjduffy

Reputation: 5215

I'm guessing the cause is JSON.parse(). HTTP requests are non-blocking in cloud code (and generally everywhere in JavaScript) so the JSON.parse() is evaluated before httpRaw has been set.

At a minimum, you need to move the parse() call and the following loop into the success handler of your HTTP request so they wait until you have a valid response. I'd suggest using Promises instead of the success/error callbacks as well.

Here's how I would go about it (warning: untested code follows ...)

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);
  });

});

Upvotes: 4

Related Questions