不断收到错误:“失败:未捕获的语法错误:<未知文件&gt :: 1中出现意外的令牌T"; (解析云代码) [英] Keep getting error: "Failed with: Uncaught SyntaxError: Unexpected token T in <unknown file>:1" (PARSE CLOUD CODE)

查看:101
本文介绍了不断收到错误:“失败:未捕获的语法错误:<未知文件&gt :: 1中出现意外的令牌T"; (解析云代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以帮助我,那么这将挽救生命!

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

(我正在使用PARSE)

(I'm using PARSE)

基本上,此解析作业尝试执行的操作是 1)查询一个称为channel的类的所有对象 2)遍历查询返回的结果"数组中的每个对象 3)调用返回JSON字符串的Google API 4)解析JSON并保存称为视频"的对象的新实例

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

问题是我不断收到错误消息: 失败:未捕获的语法错误:: 1中的意外令牌T 失败:语法错误:输入的意外结尾:

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() {

    }
});

});

推荐答案

我猜测原因是JSON.parse(). HTTP请求在云代码中(并且通常在JavaScript中的任何地方)都是非阻塞的,因此在设置httpRaw之前先评估JSON.parse().

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.

至少,您需要将parse()调用和以下循环移动到HTTP请求的成功处理程序中,以便它们等待直到收到有效响应为止.我建议也使用Promises代替成功/错误回调.

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

});

这篇关于不断收到错误:“失败:未捕获的语法错误:&lt;未知文件&gt :: 1中出现意外的令牌T"; (解析云代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆