mirror of
https://gitlab.silvrtree.co.uk/martind2000/recipes.git
synced 2025-01-11 03:15:10 +00:00
updates to stop it breaking with an empty table
This commit is contained in:
parent
79f1f5704a
commit
4803e6768e
@ -68,7 +68,7 @@ function cleaner(b) {
|
||||
}
|
||||
|
||||
function insertRecipe(obj) {
|
||||
// logger.debug(obj);
|
||||
// logger.debug(obj);
|
||||
|
||||
db.run('BEGIN TRANSACTION');
|
||||
db.run('INSERT INTO `recipes`(`url`,`html`,`reduced`,`title`) VALUES (?,?,?,?);', obj);
|
||||
@ -76,17 +76,20 @@ function insertRecipe(obj) {
|
||||
logger.debug('Insert done..');
|
||||
}
|
||||
|
||||
var doInsertRecipe = (obj) =>{
|
||||
var doInsertRecipe = (obj) =
|
||||
>
|
||||
{
|
||||
// logger.info('sendSocket: ' + JSON.stringify(obj));
|
||||
insertRecipe(obj);
|
||||
}
|
||||
|
||||
var doGetRecipe = (url) =>{
|
||||
var doGetRecipe = (url) =
|
||||
>
|
||||
{
|
||||
// logger.info('sendSocket: ' + JSON.stringify(obj));
|
||||
genericGrab(url);
|
||||
}
|
||||
|
||||
|
||||
busEmitter.on('saveRecipeData', doInsertRecipe);
|
||||
|
||||
busEmitter.on('getRecipe', doGetRecipe);
|
||||
@ -117,7 +120,7 @@ function genericGrab(url) {
|
||||
logger.debug(tdihbody.length);
|
||||
tdihbody = cleaner(tdihbody);
|
||||
logger.debug(title);
|
||||
// fs.writeFileSync(htmlfile, tdihbody.html());
|
||||
// fs.writeFileSync(htmlfile, tdihbody.html());
|
||||
obj.push(url);
|
||||
obj.push($.html());
|
||||
obj.push(tdihbody.html());
|
||||
@ -125,12 +128,11 @@ function genericGrab(url) {
|
||||
busEmitter.emit("saveRecipeData", obj);
|
||||
|
||||
}
|
||||
// fs.writeFileSync(bodyfile, $.html());
|
||||
// fs.writeFileSync(bodyfile, $.html());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
router.get('/list', function(req, res) {
|
||||
router.get('/list', function (req, res) {
|
||||
logger.debug('list..');
|
||||
|
||||
// 'select id, title from `recipes` where title is not null;'
|
||||
@ -138,23 +140,23 @@ router.get('/list', function(req, res) {
|
||||
|
||||
var out = [];
|
||||
|
||||
if (rows !== null && rows.length > 0)
|
||||
// logger.debug(err);
|
||||
// logger.debug(rows);
|
||||
if (rows !== null && rows.length > 0) {
|
||||
// logger.debug(err);
|
||||
// logger.debug(rows);
|
||||
rows.forEach(function (row) {
|
||||
|
||||
out.push({"id": row.id, "title": row.title});
|
||||
});
|
||||
}
|
||||
}
|
||||
res.writeHead(200, {"ContentType": "application/json"});
|
||||
//res.send(JSON.stringify(t));
|
||||
res.end(JSON.stringify({list:out}));
|
||||
res.end(JSON.stringify({list: out}));
|
||||
|
||||
//closeDB();
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/entry/:id', function(req, res) {
|
||||
router.get('/entry/:id', function (req, res) {
|
||||
logger.debug('entry..');
|
||||
|
||||
logger.debug(req.params.id);
|
||||
@ -166,6 +168,7 @@ router.get('/entry/:id', function(req, res) {
|
||||
// logger.debug(err);
|
||||
// logger.debug(rows);
|
||||
if (rows !== null && rows.length > 0)
|
||||
{
|
||||
rows.forEach(function (row) {
|
||||
|
||||
var d = {"id": row.id, "title": row.title};
|
||||
@ -179,15 +182,16 @@ router.get('/entry/:id', function(req, res) {
|
||||
out.push(d);
|
||||
});
|
||||
}
|
||||
res.writeHead(200, {"ContentType": "application/json"});
|
||||
//res.send(JSON.stringify(t));
|
||||
res.end(JSON.stringify({list:out}));
|
||||
res.writeHead(200, {"ContentType": "application/json"});
|
||||
//res.send(JSON.stringify(t));
|
||||
res.end(JSON.stringify({list: out}));
|
||||
|
||||
//closeDB();
|
||||
});
|
||||
//closeDB();
|
||||
});
|
||||
})
|
||||
;
|
||||
|
||||
router.post('/add', function(req, res) {
|
||||
router.post('/add', function (req, res) {
|
||||
logger.debug('add entry..');
|
||||
|
||||
var t = req.body;
|
||||
@ -203,7 +207,7 @@ router.post('/add', function(req, res) {
|
||||
}
|
||||
res.writeHead(200, {"ContentType": "application/json"});
|
||||
//res.send(JSON.stringify(t));
|
||||
res.end(JSON.stringify({adding:url}));
|
||||
res.end(JSON.stringify({adding: url}));
|
||||
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user