MeteorJS在模拟调用' code.check'的效果的同时给出错误"异常。 ReferenceError:ScrapeParser未定义"

时间:2016-05-25 12:49:04

标签: javascript meteor web-scraping meteor-blaze

我有以下问题。我正在使用MeteorJS为大学创建一个Android程序,该程序应扫描产品的条形码,然后在下一个站点显示有关它的信息(营养,盐,糖...)。我正在使用“bobbigmac:scrape-parser”软件包来抓取http://www.codecheck.info/网站,查看给定条形码的产品信息。此外,我正在使用“phonegap-plugin-barcodescanner@4.0.2”条形码扫描仪。

但是,我的代码中有一个我找不到的错误。每次扫描条形码时,都会调用“code.check”方法。在调用它之后,程序总是会出现以下错误:

Exception while simulating the effect of invoking 'code.check' ReferenceError: ScrapeParser is not defined(…) ReferenceError: ScrapeParser is not defined
    at e (http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:169:27095)
    at http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:57:12367
    at t.extend.withValue (http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:3:7325)
    at a.extend.apply (http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:57:12263)
    at Ee [as apply] (http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:143:7640)
    at a.extend.call (http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:57:11662)
    at Object.e (http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:169:25520)
    at http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:87:30879
    at http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:87:16577
    at http://localhost:12776/103fa376e3004b4c9afacff64c82bba13d4d0f29.js?meteor_js_resource=true:87:31512

奇怪的是,即使出现错误,我的程序也完成了方法中的计算并给出了正确的结果(80%的情况)。但是,有时间不进入方法,给出相同的错误。 (20%的案例) 你知道如何解决它吗?这个项目对我来说非常重要,我想我已经尝试了一切修复它但没有成功。 客户端代码:(client / main.js)

if (Meteor.isCordova) {

  Template.barcode_scanner.events({
    'click .scanButton': function (event) {

        cordova.plugins.barcodeScanner.scan(
            function (result) {
                if (confirm("You want to check the product with the following barcode? - " + result.text) == true) {
                    Meteor.call('code.check', result.text, function(err, result) {
                        if (result) {

                            Meteor.call('code.berechnen', result, function(err, score) {

                                if (score > 75) {
                                    Session.set('classVar', 'progress-bar-success');
                                } else if (score >= 50 && score <= 75) {
                                    Session.set('classVar', 'progress-bar-warning');
                                } else {
                                    Session.set('classVar', 'progress-bar-danger');
                                }       

                            });

                            Meteor.call('code.write', result, score, function(err, final) {
                                Router.go('/ScoreView');
                            });


                        }
                    });
                } else {
                    alert("You pressed Cancel!");
                }    



            }, 
            function (error) {
                alert("Scanning failed: " + error);
            }
        );

    }

  });
}

服务器代码,方法:(imports / api / tasks.js)

export const list = new Ground.Collection('list', { connection: null });

Meteor.methods({
    'code.check'(info) {

        console.log('the code is - ' + info);

        allInfo = ScrapeParser.get('http://www.codecheck.info/product.search?q='+info);    
        ref = allInfo.references[1];

        ScrapeParser.registerHelper('toInt', function(str, url) {
            return parseInt('0'+str.replace(',', ''));
        });

        ScrapeParser.registerHelper('titleLinks', function(arr, url) {
            return arr && arr.map(function(str) {
                var $ = cheerio.load(str);
                var link = $('a.title');
                return { link: link.attr('href'), title: link.text() };
            });
        });


        ScrapeParser.reset(); // Remove any/all stored parsers

        ScrapeParser.parser(ref, {
            titles: { path: 'td.c3', content: true, multi: true },
            prozent: { path: 'td.c-2', content: true, multi: true },
            packung: { path: 'body > div.page.product-info > div.float-group > div.page-col-1-2-left > div.block.prod-basic-info > div > div:nth-child(2) > p:nth-child(2)', content: true, multi: true },
            name: { path: 'div.page-title-headline h1', content: true, multi: true },
        });



        ScrapeParser.resetExcept([ref]); // Remove stored parsers except those in array

        values = ScrapeParser.get(ref); 

        values.packung[0] = values.packung[0].replace(",", ".");

        values.name[0] = values.name[0].trim();

        values.titles[4] = values.prozent[0];
        values.titles[5] = values.prozent[1];
        values.titles[6] = values.prozent[3];
        values.titles[7] = values.packung[0];
        values.titles[8] = values.name[0];


        return values;

    },
});

“code.berechnen”方法只进行简单的数学运算(+ / - *),“code.write”将结果保存到GroundDB数据库中。 您可以在此存储库中找到完整代码:https://github.com/LukasNavickas/meteor-reference-error

如果您想尝试一下,请克隆存储库并尝试在模拟器,Android设备或iOS设备上启动它。

您是否有任何想法如何解决此错误并在100%的情况下获得结果?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你不需要这里的抓包的开销。 Meteor有一个内置的http [1]模块供您进行请求,使用生成的HTML,您可以使用cheerio [2]来提取您想要的任何选择器。

免责声明:除了一些内容摘要和标记之外,ScrapeParser的基础库似乎是我的scrape包[3],其中正是这样 [ 4]。但是,我现在已经很长时间没有维护这个包了,并且打开了几个bug报告,所以只需用几行代码就可以自己编写,并从抓取抽象中删除所有可能的问题。

[1] https://themeteorchef.com/snippets/using-the-http-package/#tmc-get-requests

[2] https://github.com/cheeriojs/cheerio

[3] https://github.com/Anonyfox/meteor-scrape

[4] https://github.com/Anonyfox/meteor-scrape/blob/master/lib/parse-website.coffee.md