检查变量是否存在然后更新

时间:2018-07-01 07:18:07

标签: javascript mysql node.js twitch

我想在我的mysql数据库上的表中搜索,如果确实找到了列中的变量,我希望它每分钟更新一次。如果不存在,我希望它创建它,然后它将被更新。我还没有创建部分或更新部分,如果有人可以帮助我做到这一点将是很棒的。这是我的代码:

var request = require("request")
var sql = require("mssql")

var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'xxx.xx.xx.xx',
  user     : 'root',
  password : 'xxx',
  database : 'points'
});


var Found = false;
var url = "https://tmi.twitch.tv/group/user/kong_plays/chatters"
var viewerAmount = 0;
var viewers = [];
var val = 0;

connection.connect((err) => {
  if (err) {
    console.log("Error connecting to database")
  }
  console.log('Connected to database!');
});


request({
    url: url,
    json: true
}, function (error, response, body) {
    if (!error && response.statusCode === 200) {
        viewers = body.chatters.viewers;
        console.log(viewers);
        viewerAmount = viewers.length;


    }
})

console.log(viewerAmount)
    function getPoints(){
              while(viewerAmount >= 0) {

              }
        }
        setInterval(function(){
            getPoints()}, 3000)

connection.query('SELECT * FROM points', (err,rows) => {

if(err) throw err;
rows.forEach( (row) => {
  viewerCount = viewerAmount
  console.log(viewerCount)
  while(viewerCount >= 0){
    console("1")
    viewerCount = viewerCount - 1;
    if (viewers[viewerCount] === row.twitchName) {
      console.log("Found "+viewers[viewerCount]+" in the database already. Ignoring");
      Found = true;
    }else if(viewerCount > 0){
      viewerCount = viewerCount - 1;        
    }else if (viewerCount === 0){
      console.log("Not found!")
    }
  }
});

});

0 个答案:

没有答案