错误:在已经将握手排队

时间:2018-03-14 11:20:54

标签: mysql node.js

我正在从不同来源获取新闻资讯&添加到DB但我得到以下异常

  

错误:在已经将握手排队后无法将握手排入队列。       在Protocol._validateEnqueue(/home/vikas/node_modules/mysql/lib/protocol/Protocol.js:210:16)       在Protocol._enqueue(/home/vikas/node_modules/mysql/lib/protocol/Protocol.js:139:13)       在Protocol.handshake(/home/vikas/node_modules/mysql/lib/protocol/Protocol.js:52:23)       在Connection.connect(/home/vikas/node_modules/mysql/lib/Connection.js:130:18)       at storeNewsFeedsInDB(/ home / vikas / NodeJS Practice / fetchNews.js:42:7)       在newsapi.v2.everything.then.response(/ home / vikas / NodeJS Practice / fetchNews.js:33:9)       在tryCatcher(/home/vikas/node_modules/bluebird/js/release/util.js:16:23)       在Promise._settlePromiseFromHandler(/home/vikas/node_modules/bluebird/js/release/promise.js:512:31)       在Promise._settlePromise(/home/vikas/node_modules/bluebird/js/release/promise.js:569:18)       在Promise._settlePromise0(/home/vikas/node_modules/bluebird/js/release/promise.js:614:10)       在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)       在Immediate.Async.drainQueues(/home/vikas/node_modules/bluebird/js/release/async.js:17:14)       在runCallback(timers.js:637:20)       在tryOnImmediate(timers.js:610:5)

以下是我的代码

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");
    });
  });
}

Nodejs&amp; Mysql,请帮帮我。

1 个答案:

答案 0 :(得分:2)

你正在(嵌套)循环中调用storeNewsFeedsInDB,每次访问它时都会调用con.connect,这就是你得到“排队握手”错误的原因。查看one of mysql npm package test suites可能会显露出来,因为一旦进行了两次con.connect次调用,就会抛出错误。

您应该善于删除代码中的con.connect来电,因为任何query调用都隐式建立了连接as the docs say