배희수

Resolve error

1 var express = require('express'); 1 var express = require('express');
2 var router = express.Router(); 2 var router = express.Router();
3 var db = require('../lib/db'); 3 var db = require('../lib/db');
4 +require('date-utils');
5 +
6 +var newDate = new Date();
4 7
5 /* GET home page. */ 8 /* GET home page. */
6 router.get('/', function(req, res, next) { 9 router.get('/', function(req, res, next) {
...@@ -11,38 +14,52 @@ router.get('/', function(req, res, next) { ...@@ -11,38 +14,52 @@ router.get('/', function(req, res, next) {
11 var wsArr = new Array(); // 풍속 14 var wsArr = new Array(); // 풍속
12 var rainArr = new Array(); // 강우량 15 var rainArr = new Array(); // 강우량
13 var probArr = new Array(); // 사망 확률 16 var probArr = new Array(); // 사망 확률
17 + var dataLen = 0; // 데이터 개수
14 var empty = 0; // 초기값 유뮤, 0 : 자료 있음, 1 : 자료 없음 18 var empty = 0; // 초기값 유뮤, 0 : 자료 있음, 1 : 자료 없음
15 var sql = ""; // 쿼리 19 var sql = ""; // 쿼리
16 var index; 20 var index;
17 21
18 // 이전 10분간 데이터 찾기 22 // 이전 10분간 데이터 찾기
23 + sql = "SELECT * FROM weatherInfo WHERE time >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -10 MINUTE), '%Y-%m-%d %H:%i:%s')";
24 + db.query(sql, function(err, rows, fields){
25 + if (err) {
26 + console.log(err);
27 + } else {
28 + if (rows.length == 0) {
29 + empty = 1;
30 + } else {
31 + dataLen = rows.length;
32 + for (index = 0; index < rows.length; index++){
33 + var temp = rows[index].time.getMinutesBetween(newDate);
19 34
20 - sql = "SELECT * FROM weatherInfo WHERE time >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -10 MINUTE), '%Y-%m-%d %H:%i:%s')"; 35 + if(temp == index){
21 - 36 + probArr.push(rows[index].prob);
22 - db.query(sql, function(err, rows, fields){ 37 + time.push(rows[index].time);
23 - if (err) { 38 + ptArr.push(rows[index].temperature);
24 - console.log(err); 39 + wsArr.push(rows[index].wind);
25 - } else { 40 + rainArr.push(rows[index].rain);
26 - if (rows.length != 10) { 41 + }
42 + else {
27 empty = 1; 43 empty = 1;
28 - } else { 44 + break;
29 - probArr.push(rows[0].prob);
30 - time.push(rows[0].time);
31 - ptArr.push(rows[0].temperature);
32 - wsArr.push(rows[0].wind);
33 - rainArr.push(rows[0].rain);
34 } 45 }
35 -
36 - res.render('index', {
37 - empty,
38 - time,
39 - ptArr,
40 - wsArr,
41 - rainArr,
42 - probArr
43 - });
44 } 46 }
45 - }) 47 +
48 + if (empty == 1 && probArr.length != 0)
49 + empty = 0;
50 + }
51 +
52 + res.render('index', {
53 + empty,
54 + time,
55 + ptArr,
56 + wsArr,
57 + rainArr,
58 + probArr,
59 + dataLen
60 + });
61 + }
62 + })
46 }); 63 });
47 64
48 module.exports = router; 65 module.exports = router;
......