Node.JS即使它是

时间:2017-04-05 20:19:10

标签: javascript mysql node.js

所以我的node.js代码中出现“No database selected”错误。但是,它被选中,并且当我第一次启动它时,连接语句确实返回一条成功的消息。我的插入方式遵循本教程(https://www.sitepoint.com/using-node-mysql-javascript-client/

我还使用npm为节点安装了MySQL模块,虽然我怀疑即使它在模块下也仍然无法正常运行,即使我把数据库放到不存在的东西上它仍然会说一个成功的连接。所以我猜两个问题:如何确保我实际上正确地下载了MySQL组件,我在我的机器人中犯了什么错误?代码如下。

// Dependencies =========================
var  
    twit = require('twit'),
    config = require('./config');

var mysql = require("mysql");

var con = mysql.createConnection(
{    
   host: "localhost",
   user: "root",
   password: "Jaljap2732!",
   dateStrings: true,
   datebase: "rawdata"
});



var Twitter = new twit(config);

con.connect(function(err) {
   if(err) {
       console.log("Sorry Fam, error connecting");
   } else {
       console.log("Connection Successful")
   }
});



var searchTweets = function(){  
  var params = {
      q: 'since:2017-04-01',  // REQUIRED
      result_type: 'recent',
      count:'10',
      lang: 'en'
  }
  // find the tweet
  Twitter.get('search/tweets', params, function(err,data){

    // find tweets
    var tweet = data.statuses;

      //console.log(tweet);
   for(var result in tweet) {
       console.log("text: " + tweet[result].text);
       var created_at = tweet[result].created_at;
       var date = created_at.split(" ");
       var month = date[1];

       var sqlDate = date[5] + "-" + monthToInt(month) + "-" + date[2]; 


       var data = {
         tweet:  tweet[result].text,
         posted: sqlDate,
         User_idUser:  tweet[result].user.screen_name 
       };

       con.query('INSERT INTO tweet SET ?', data, function(err, rows) {
          if(err) throw err;
           else {
               console.log("Data from DB");
               console.log(rows);
           }
       });

       console.log("Posted at: " + date[1] + " " + date[2] + " " + date[5]);
       console.log("Converted Date: " + monthToInt(month));
       console.log("User: " + tweet[result].user.screen_name);
   }

  });
}
// grab & 'favorite' as soon as program is running...
searchTweets();  
// 'favorite' a tweet in every 15 minutes
setInterval(searchTweets, 900000);


function monthToInt(month) {

    //We will return a string representation of the month
    //using numbers in order to insert it into our SQL table. 


    var result = "";

    switch(month) {
        case "Jan":
            console.log("Janaury: 01");
            result = "01";
            break;
        case "Feb":
            console.log("February: 02");
            result = "02";
            break;
        case "Mar":
            console.log("March: 03");
            result = "03";
            break;
        case "Apr":
            console.log("April: 04");
            result = "04";
            break;
        case "May":
            console.log("May: 05");
            result = "05";
            break;
        case "Jun":
            console.log("Jun: 06");
            result = "06";
            break;
        case "Jul":
            console.log("Jul: 07");
            result = "07";
            break;
        case "Aug":
            console.log("August: 08");
            result = "08";
            break;
        case "Sept":
            console.log("September: 09");
            result = "09";
            break;
        case "Sep":
            console.log("September: 09");
            result = "09";
            break;
        case "Oct":
            console.log("October: 10");
            result = "10";
            break;
        case "Nov":
            console.log("November: 11");
            result = "11";
            break; 
        case "Dec":
            console.log("December: 12");
            result = "12";
            break;
    }

    return result;
}

1 个答案:

答案 0 :(得分:0)

这是一个修复的黑客,但我只是强迫它使用另一个查询使用有问题的数据库。我会非常感谢一个可靠的答案,而不是一个模糊的评论答案,描述竞争条件,因为我认为代码按顺序执行。 searchCode内部的任何内容都不应该在另一个东西之前执行。