我使用node.js编写游戏,我使用Postgres包作为名为pg的节点。 我插入数据库的所有内容都有效。也可以检索插入的数据。即使重新启动dyno,数据仍然存在。
插入的数据不超过几行。
但是在一天左右之后,表中不存在插入的数据。我阅读了Heroku文档,但无法找到原因。
出了什么问题?
我正在使用Heroku的免费等级和Postgres的爱好开发计划。
修改
我的数据库代码如下:
// I need to delete the rows from the database which are one day old, one week old and the third table not at all old
var flushTable = function(tableName, dateObj, callback) {
var client = null, query = null;
var strDate = getDateString(dateObj);
client = new pg.Client(connectionString);
client.connect();
query = client.query('DELETE FROM '+tableName+' WHERE latest_date<TO_DATE(\''+strDate+'\',\'YYYY-MM-DD\');', function(err, result) {
if(err) {
console.log(err);
client.end();
return;
}
client.end();
callback();
});
};
var createNonExistent = function(tableName, callback) {
var client = null, query = null;
client = new pg.Client(connectionString);
client.connect();
query = client.query('CREATE TABLE IF NOT EXISTS '+tableName+' (username VARCHAR(16) PRIMARY KEY NOT NULL, score INTEGER NOT NULL, latest_date DATE NOT NULL, latest_time TIME NOT NULL, ip VARCHAR(30) NOT NULL);', function(err, result) {
if (err) {
console.log(err);
client.end();
return;
}
console.log('Table Created');
client.end();
callback();
});
}
var readFromTable = function(tableName, callback) {
flushTable(tableName, new Date(), function(){
var client = null, query = null;
client = new pg.Client(connectionString);
client.connect();
query = client.query('SELECT * FROM '+tableName+' ORDER BY score DESC, latest_date DESC, latest_time DESC;', function(err, result){
if(err) {
console.log(err);
client.end();
return;
}
console.log('Read values');
console.log(result.rows);
client.end();
callback(result);
});
});
};
答案 0 :(得分:0)
我想到了错误,我没有更改代码中的日期,所以我添加了另一个名为flushAll()的函数,我在读写之前调用了它。
函数flushAll()正确删除表,而不像readTable()函数中的删除那样。
以下是修改后的代码:
var flushTable = function(tableName, dateObj, callback) {
var client = null, query = null;
var strDate = getDateString(dateObj);
client = new pg.Client(connectionString);
client.connect();
query = client.query('DELETE FROM '+tableName+' WHERE latest_date<TO_DATE(\''+strDate+'\',\'YYYY-MM-DD\');', function(err, result) {
if(err) {
console.log(err);
client.end();
return;
}
client.end();
callback();
});
};
var createNonExistent = function(tableName, callback) {
var client = null, query = null;
client = new pg.Client(connectionString);
client.connect();
query = client.query('CREATE TABLE IF NOT EXISTS '+tableName+' (username VARCHAR(16) PRIMARY KEY NOT NULL, score INTEGER NOT NULL, latest_date DATE NOT NULL, latest_time TIME NOT NULL, ip VARCHAR(30) NOT NULL);', function(err, result) {
if (err) {
console.log(err);
client.end();
return;
}
console.log('Table Created');
client.end();
callback();
});
}
var flushAll = function(callback) {
flushTable('today',new Date(), function(){
var dateObj = new Date();
var strDate = getDateString(dateObj);
while (dateObj.getDay() != 0) {
dateObj.setDate(dateObj.getDate()-1);
}
flushTable('thisweek',new Date(), function(){
callback();
});
});
}
var readFromTable = function(tableName, callback) {
var client = null, query = null;
client = new pg.Client(connectionString);
client.connect();
query = client.query('SELECT * FROM '+tableName+' ORDER BY score DESC, latest_date DESC, latest_time DESC;', function(err, result) {
if(err) {
console.log(err);
client.end();
return;
}
console.log('Read values');
console.log(result.rows);
client.end();
callback(result);
});
};
exports.readLeaderBoards = function(callback) {
var boards = [];
flushAll(function(){
createNonExistent('alltime', function(){
createNonExistent('thisweek', function(){
createNonExistent('today', function(){
readFromTable('today', function(result) {
boards['today'] = result.rows;
readFromTable('thisweek', function(result) {
boards['thisweek'] = result.rows;
readFromTable('alltime', function(result) {
boards['alltime'] = result.rows;
console.log(boards['today']);
callback(boards);
});
});
});
});
});
});
});
};