0
votes

I am fetching news feeds from different sources & adding to DB but I get below exception

Error: Cannot enqueue Handshake after already enqueuing a Handshake. at Protocol._validateEnqueue (/home/vikas/node_modules/mysql/lib/protocol/Protocol.js:210:16) at Protocol._enqueue (/home/vikas/node_modules/mysql/lib/protocol/Protocol.js:139:13) at Protocol.handshake (/home/vikas/node_modules/mysql/lib/protocol/Protocol.js:52:23) at Connection.connect (/home/vikas/node_modules/mysql/lib/Connection.js:130:18) at storeNewsFeedsInDB (/home/vikas/NodeJS Practice/fetchNews.js:42:7) at newsapi.v2.everything.then.response (/home/vikas/NodeJS Practice/fetchNews.js:33:9) at tryCatcher (/home/vikas/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/vikas/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/home/vikas/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/home/vikas/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/home/vikas/node_modules/bluebird/js/release/promise.js:693:18) at Async._drainQueue (/home/vikas/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/vikas/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/vikas/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:637:20) at tryOnImmediate (timers.js:610:5)

Below is my code

const NewsAPI = require('newsapi');
const newsapi = new NewsAPI('*****************************');

var mysql = require('mysql');
var con = mysql.createConnection({
  host: "****",
  user: "****",
  password: "****",
  database: "****"
});

var channels = ['bbc-news', 'the-verge', 'reuters', 'cnbc', 'bloomberg', 'economist', 'nytimes'];
var topics = ['technology', 'business', 'finance', 'tech', 'economics', 'company'];

for (var i = 0; i < channels.length; i++) {
  for (var j = 0; j < topics.length; j++) {
    newsapi.v2.everything({
      q: topics[j],
      // q: 'technology',
      sources: channels[i],
      // sources: 'bloomberg',
      // domains: 'bbc.co.uk, techcrunch.com',
      from: '2018-03-14',
      to: '2018-03-14',
      language: 'en',
      sortBy: 'relevancy',
      pageSize:100,
      page: 1
    }).then(response => {
      // console.log(response);
      for (var i = 0; i < response.articles.length; i++) {
        // console.log(response.articles[i].title);
        storeNewsFeedsInDB(response.articles[i].title, 
                            response.articles[i].description, 
                              response.articles[i].url);
      }
    });
  }
}

function storeNewsFeedsInDB(title, description, url) {
  con.connect(function(err) {
    if (err) throw err;
    console.log("Connected!");

    //Insert a record in the "newsfeeds" table:
    var sql = "INSERT IGNORE INTO newsfeeds (`title`, `description`, `url`) VALUES ('" 
                + title.replace(/'/g, "\\'") + "', '" + description.replace(/'/g, "\\'") + "', '" + url + "')";
                console.log(sql);
    con.query(sql, function (err, result) {
      if (err) throw err;
      console.log("1 record inserted");
    });
  });
}

New to Nodejs & Mysql, please help me.

1

1 Answers

4
votes

You are calling storeNewsFeedsInDB from inside a (nested) loop, and each time it's accessed you call con.connect, that's why you get that "enqueued handshake" error. A look at one of mysql npm package test suites can be revealing, as the error is thrown as soon as two successive con.connectcalls are made.

You should be good by just removing that con.connect call from your code, as any query call established the connection implicitly, as the docs say.