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


#1

I have the following problem. I’m creating an Android program for university using MeteorJS that should scan a barcode of a product and then show information (nutrition, salt, sugar…) about it in the next site. I’m using “bobbigmac:scrape-parser” package to scrape http://www.codecheck.info/ website for the product information with the given barcode. Moreover, I’m using “phonegap-plugin-barcodescanner@4.0.2” barcode scanner.

However, there is an error in my code which I cannot find. Every time I scan a barcode, “code.check” method is called. After calling it the program always gives the following error:

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

It is strange that even with the error my program completes the calculations in the method and gives me the correct results (80% of the cases). However, times to times it doesn’t enter the method, giving the same error. (20% of the cases) Have you an idea how to fix it? This project is very important to me and I think I have tried everything fixing it but didn’t succeed.

Client code: (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);
            }
        );
    }  
});
}

Server code, methods: (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” method just does simple math (+/-*) calculations and “code.write” saves the results into GroundDB database. You can find the full code in this repository: https://github.com/LukasNavickas/meteor-reference-error

If you want to try it, clone the repository and try launching it on the emulator, android device or iOS device.

Do you have any ideas how could I fix this error and get the results in 100% of the cases?

Thank you in advance!