connection is defined in main.js not in match.js
Modules isolate the scope of the variables.
Since match.js exports a function you can pass the connection to the
function.

On Sat, Mar 19, 2016 at 11:16 AM, jeg <[email protected]> wrote:

> Hi all,
>
> error message when match.js is called with  insert
>
> thanks
>
> Eric
>
> match.js:53
>           connection.query('insert into MYTABLE set ' + wREQUEST,
> function(err, res) {
>           ^
>
> ReferenceError: connection is not defined
>
>
> ***
> main.js
>
> var request = require('request');
> var cheerio = require('cheerio');
>
> var mysql      = require('mysql');
> var connection = mysql.createConnection({
>   host     : 'localhost',
>   user     : 'admin',
>   password : 'secret',
>   database : 'mydb'
> });
>
> connection.connect();
>
> request('http://url/en', function (error, response, html) {
>   if (!error && response.statusCode == 200) {
>     var $ = cheerio.load(html);
>     $('div.expander-button').each(function(i, element){
>       var a = $(this);
>       var zurl = a.next().children().children().attr('href');
>       // Our parsed meta data object
>       var metadata = {
> url: zurl
>       };
>       //console.log(zurl);
>  var a = require('./match')(zurl);
>  a.extractValue();
>     });
>   }
> });
>
> connection.end();
>
>
>
> ***
> match.js
>
> module.exports = function(zurl) {
>   return {
>   extractValue: function() {
> ....
> ....
> ....
>
>
>  connection.query('insert into MYTABLE  set ' + wREQUEST, function(err,
> res) {
>       if (err) throw err;
>  } ) ;
>
>
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/38690d8e-9440-4cbd-bcf3-3b72e892eb98%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/38690d8e-9440-4cbd-bcf3-3b72e892eb98%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
ema
http://ema.codiceplastico.com <http://blog.codiceplastico.com/ema>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAKxFu7QErdB-eTX9zj9P2u%2BQLEGDSUAPkUzLaGpeqm59zdcNPw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to