so getting "no database selected" error in node.js code. however, selected, , connection statement return successful message when first launch it. way of inserting follows tutorial (https://www.sitepoint.com/using-node-mysql-javascript-client/)
i installed mysql module node using npm, though suspect though under module still not functioning correctly if put database doesn't exist still successful connection. guess 2 part question: how make sure downloaded mysql component correctly, , making mistake in bot? code below.
// 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 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 tweet set ?', data, function(err, rows) { if(err) throw err; else { console.log("data 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' program running... searchtweets(); // 'favorite' tweet in every 15 minutes setinterval(searchtweets, 900000); function monthtoint(month) { //we return string representation of month //using numbers in order insert 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; }
it hack of fix, forced use database in question using query. appreciate solid answer, , not vague comment answer describing race condition way see code executes sequentially. nothing in inside of searchcode should ever execute before thing.
Comments
Post a Comment