2017-04-05 49 views
0

因此,我在我的node.js代碼中收到「沒有數據庫選擇」錯誤。但是,它已被選中,並且連接語句在首次啓動時會返回成功的消息。我的插入方式如下本教程(https://www.sitepoint.com/using-node-mysql-javascript-client/Node.JS沒有選擇數據庫,即使它是

我也使用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; 
} 
+0

你不需要在db連接回調裏面做所有的tweeting嗎? –

+0

也許?說實話,我對node.js相當陌生,而且我仍然處於回調的狀態。 – SomeStudent

+0

爲了確保您正確下載了它,您可以進入名爲'node_modules'的文件夾,刪除名爲'mysql'的文件夾,然後運行'npm install mysql',這將重新安裝該軟件包。 –

回答

0

這是一個修復的破解,但我只是強迫它使用另一個查詢問題的數據庫。我會認真地感謝一個可靠的答案,而不是一個模糊的評論答案,描述了競爭條件,就像我看到代碼順序執行的方式。 searchCode內部的任何內容都不應該在另一件事之前執行。