我正在通过cron.Job()每天删除数据并将其插入数据库。 但是插入一些数据后,服务器上的数据库结果不会刷新。
我在这里找到了2个答案。但是不能理解其中一个而另一个也老了。
在代码中我首先截断表,然后我插入新数据。但是当我发送请求时,它会向我显示旧数据。
在get_pharmacy.js中,我只是做一些排序操作并向用户发送响应。
这是我的代码:
var port = 4000;
var express = require("express");
var bodyParser = require('body-parser');
var get_pharmacy = require("./routes/get_pharmacy.js");
var app = express();
var path = require("path");
var cron = require('cron');
app.use('/static',express.static(path.join(__dirname, 'static')));
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/get_pharmacy", get_pharmacy);
var job1 = new cron.CronJob({
cronTime: '55 59 21 * * 1-7', // 00 00 22
onTick: function() {
var deleter = require("/home/ahmet/development_nuri/deleter.js");
},
start: false,
timeZone: 'America/Los_Angeles'
});
var job2 = new cron.CronJob({
cronTime: '00 00 22 * * 1-7', // 00 00 22
onTick: function() {
var istanbul = require("/home/ahmet/development_nuri/scrap.js");
},
start: false,
timeZone: 'America/Los_Angeles'
});
job1.start();
job2.start();
process.on('uncaughtException', function (err) {
console.log('UNCAUGHT', err.stack);
});
app.listen(port);
get_pharmacy.js(请求来到这里)
var express = require("express");
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "159487aa",
database: "eczane",
debug: false
});
var cities = [];
const router = express.Router();
setInterval(function () {
con.query('SELECT 1');
}, 5000);
function swap(a,b)
{
var temp=a;
}
function sortMeThender(data)
{
var array = []
for(var i=0; i<data.length; i++)
{
for(var x=0; x<data.length; x++)
{
if(data[i]["distance"] < data[x]["distance"])
{
var temp=data[x];
data[x]=data[i];
data[i]=temp;
}
}
}
array.push(data[0]);
array.push(data[1]);
array.push(data[2]);
array.push(data[3]);
array.push(data[4]);
return array;
}
router.route("/")
.post(function(request, response) {
var lat = request.body.lat;
var long = request.body.long;
con.query("select name,adress,town,city,phone,latitude,longitude from eczane", function (err, result) {
if (err) throw err;
cities.push(result);
for (var i = 0; i < cities[0].length; i++) {
cities[0][i]["distance"] = getDistanceFromLatLonInKm(lat, long, cities[0][i]["latitude"], cities[0][i]["longitude"]);
}
response.send(sortMeThender(cities[0]));
});
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d * 1000;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
});
module.exports = router;
答案 0 :(得分:1)
get_pharmacy.js中的cities变量是全局的。将其更改为本地(在post方法中使用它)。