module export mySQL - ReferenceError: connection is not defined

2,254 views
Skip to first unread message

jeg

unread,
Mar 19, 2016, 10:26:20 AM3/19/16
to nodejs
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;
 } ) ;
 

Emanuele DelBono

unread,
Mar 19, 2016, 1:20:01 PM3/19/16
to nod...@googlegroups.com
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.

--
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 nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/38690d8e-9440-4cbd-bcf3-3b72e892eb98%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

jeg

unread,
Mar 20, 2016, 2:41:33 PM3/20/16
to nodejs
it's works well

Thanks Ema
Reply all
Reply to author
Forward
0 new messages