MeteorJS gives an error "Exception while simulating the effect of invoking 'code.check' ReferenceError: ScrapeParser is not defined"

MeteorJS gives an error "Exception while simulating the effect of invoking 'code.check' ReferenceError: ScrapeParser is not defined"

我有以下问题。我正在使用 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% 的情况下获得结果吗?

提前致谢!

我认为您在这里不需要抓取包的开销。 Meteor 有一个内置的 http [1] 模块供您执行请求,并且根据结果 HTML,您可以使用 cheerio [2] 提取您想要的任何选择器。

免责声明: 除了一些内容总结和标记,ScrapeParser 的底层库似乎是我的抓取包 [3], 确实正是 [4]。然而,我已经有一段时间没有维护这个包了,并且有几个错误报告打开了,所以只需用几行代码滚动你自己的并从抓取抽象中删除所有可能的问题。

[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