【问题标题】:MeteorJS gives an error "Exception while simulating the effect of invoking 'code.check' ReferenceError: ScrapeParser is not defined"MeteorJS 给出错误“模拟调用'code.check'ReferenceError 的效果时出现异常:未定义ScrapeParser”
【发布时间】:2016-09-23 02:10:06
【问题描述】:

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

提前谢谢你!

【问题讨论】:

    标签: javascript meteor web-scraping meteor-blaze


    【解决方案1】:

    我认为您在这里不需要抓取包的开销。 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

    【讨论】:

    • 非常感谢您的回复。你能给我一个简单的例子,我如何将cheerio(据我了解,它是一个npm pacakge?)与内置的http方法集成?假设用任何类名抓取一个简单的网站/获取标签的内容?
    • 由于直接支持Meteor 1.3 NPM,看看guide.meteor.com/using-npm-packages.html#using-npm
    猜你喜欢
    • 2017-09-27
    • 2015-11-13
    • 2014-10-12
    • 2015-06-17
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    相关资源
    最近更新 更多