req ist not defined even when it's filled with values - javascript

Im updating some of my data inside my database with a REST API. Therefore I created a route with POST (I use it for both) like this and call it with a submit button:
router.post('/add/edit/update', function (req, res, next) {
console.log(req.body);
var sql = 'UPDATE gerichte SET gericht = ' + reg.body.gericht + ', Kalorien = ' + reg.body.Kalorien + ', preis_id = ' + req.body.preis + ', kategory_id = ' + req.body.kategorie + ', allergene = ' + req.body.allergene + ' WHERE g_id = ' + req.body.g_id + ';';
conn.query(sql, function (err, data) {
if (err) throw err;
});
res.redirect('/add/edit'); // redirect to user form page after inserting the data
});
But I always get the error req not found when I sumbit the form. I tried printing the req object but it's filled nicely:
[Object: null prototype] {
g_id: '1',
gericht: 'Schnitzel mit Pommes',
Kalorien: '1400',
preis: '1',
kategorie: '5',
allergene: 'dsd'
}
Any ideas how to fix this problem?

It's req (with a lower-case Q), not reg (with lower-case G).
Change
var sql = 'UPDATE gerichte SET gericht = ' + reg.body.gericht + ', Kalorien = ' + reg.body.Kalorien + ', preis_id = ' + req.body.preis + ', kategory_id = ' + req.body.kategorie + ', allergene = ' + req.body.allergene + ' WHERE g_id = ' + req.body.g_id + ';';
to
var sql = 'UPDATE gerichte SET gericht = ' + req.body.gericht + ', Kalorien = ' + req.body.Kalorien + ', preis_id = ' + req.body.preis + ', kategory_id = ' + req.body.kategorie + ', allergene = ' + req.body.allergene + ' WHERE g_id = ' + req.body.g_id + ';';

var sql = 'UPDATE gerichte SET gericht = ' + reg.body.gericht + ', Kalorien = ' + reg.body.Kalorien + ', preis_id = ' + req.body.preis + ', kategory_id = ' + req.body.kategorie + ', allergene = ' + req.body.allergene + ' WHERE g_id = ' + req.body.g_id + ';';
The error might be reg not defined because:
In the above line of your code, you've been using reg.body, when there is no reg defined.
Kindly correct it and see if the issue is resolved or not.

Related

mysql query on javascript gets ER_PARSE_ERROR but it worked in phpmyadmin

var query =
'SELECT '
+ prefix + 'threads.tid as _tid, '
+ prefix + 'threads.fid as _cid, '
+ prefix + 'threads.firstpost as _pid, '
+ prefix + 'threads.views as _viewcount, '
+ prefix + 'threads.subject as _title, '
+ prefix + 'threads.dateline as _timestamp, '
+ prefix + 'threads.sticky as _pinned, '
+ prefix + 'posts.uid as _uid, '
+ prefix + 'posts.tid as _post_tid, '
+ prefix + 'posts.message as _content, '
+ 'IF(filetype like "image%", CONCAT("/uploads/files/MyBBAttachment/", attachname), NULL) as _images, '
+ 'IF(filetype like "image%", GROUP_CONCAT(filename SEPARATOR ","), NULL) as _imgnames, '
+ 'IF(filetype not like "image%", CONCAT("/uploads/files/MyBBAttachment/", attachname), NULL) as _attachments, '
+ 'IF(filetype not like "image%", GROUP_CONCAT(filename SEPARATOR ","), NULL) as _attachnames '
+ 'FROM ' + prefix + 'posts LEFT JOIN ' + prefix + 'attachments ON (' + prefix + 'posts.pid = ' + prefix + 'attachments.pid), ' + prefix + 'threads '
+ 'WHERE ' + prefix + 'threads.firstpost=' + prefix + 'posts.pid '
+ 'GROUP BY ' + prefix + 'posts.pid'
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
Above query doesn't work in Node.js, I get following error:
{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0,600000' at line 1
But when I run similar thing in PHPMyAdmin, it works:
SELECT mybb_threads.tid as _tid, mybb_threads.fid as _cid, mybb_threads.firstpost as _pid, mybb_threads.views as _viewcount, mybb_threads.subject as _title, mybb_threads.dateline as _timestamp, mybb_threads.sticky as _pinned, mybb_posts.uid as _uid, mybb_posts.tid as _post_tid, mybb_posts.message as _content
FROM mybb_posts LEFT JOIN mybb_attachments ON mybb_posts.pid = mybb_attachments.pid, mybb_threads
where mybb_threads.firstpost=mybb_posts.pid
group by mybb_posts.pid
LIMIT 0, 100
How do I make var query work?
You're missing a whitespace before the limit keyword:
+ (start >= 0 && limit >= 0 ? ' LIMIT ' + start + ',' + limit : '');
// Here -----------------------^

NodeJS SQL returning 'Cannot read property 'x' of undefined' in for loop

I'm trying to run through each post in a MySQL table, get the username, run a separate query in a separate table getting information from that user such as profile img, etc and then put it in JSON.
This work up until I try and add another query in for getting the user information, as then it returns
'Cannot read property 'postId' of undefined'
I've tried many workarounds to this, but none of them have worked.
Here's my code:
var postLocation = req.query.postLocation.replace(regex, escaper);
connection.query("SELECT * FROM `posts` WHERE (postLocation = '" + postLocation + "' OR postLocation = 'Global') AND postDeleted = 'no' ORDER BY postId DESC;", function (err1, rows1, fields1) {
if(err1){
res.send(err1);
} else if(!rows1.length){
res.send('{"rapids": [{"response": "This post does not exist."}]}');
} else {
var queryString = "";
var queryTimes = 0;
var profileURL = null;
for(i = 0; i < rows1.length; i++){
queryTimes++;
connection.query("SELECT * FROM `users` WHERE username = '" + rows1[0].postBy + "' ORDER BY postId DESC;", function (err2, rows2, fields2) {
queryString = queryString + '{"postId": "' + rows1[i].postId + '", "post": "' + rows1[i].post + '", "postBy": "' + rows1[i].postBy + '", "postLikes": ' + rows1[i].postLikes + ', "postByProfileImgURL": "' + rows1[i].postByProfileImgURL + '", "postDate": "' + rows1[i].postDate + '", "postViews": ' + rows1[i].postViews + ', "postDeleted": "' + rows1[i].postDeleted + '"';
if(queryTimes == rows1.length){
queryString = queryString + '}';
} else {
queryString = queryString + '}, ';
}
});
}
res.send('{"rapids": [' + queryString + ']}');
}
});
Thanks!
rows1[i].postId is in a different context there. You'll have to pass it into the context that your inner query runs in.
var postLocation = req.query.postLocation.replace(regex, escaper);
connection.query("SELECT * FROM `posts` WHERE (postLocation = '" + postLocation + "' OR postLocation = 'Global') AND postDeleted = 'no' ORDER BY postId DESC;", function (err1, rows1, fields1) {
if(err1){
res.send(err1);
} else if(!rows1.length){
res.send('{"rapids": [{"response": "This post does not exist."}]}');
} else {
var queryString = "",
queryTimes = 0,
profileURL = null;
for(i = 0; i < rows1.length; i++){
queryTimes++;
connection.query("SELECT * FROM `users` WHERE username = '" + rows1[0].postBy + "' ORDER BY postId DESC;", function (rows1, err2, rows2, fields2) {
queryString = queryString + '{"postId": "' + rows1[i].postId + '", "post": "' + rows1[i].post + '", "postBy": "' + rows1[i].postBy + '", "postLikes": ' + rows1[i].postLikes + ', "postByProfileImgURL": "' + rows1[i].postByProfileImgURL + '", "postDate": "' + rows1[i].postDate + '", "postViews": ' + rows1[i].postViews + ', "postDeleted": "' + rows1[i].postDeleted + '"';
if(queryTimes == rows1.length){
queryString = queryString + '}';
} else {
queryString = queryString + '}, ';
}
}.bind(this, rows1));
}
res.send('{"rapids": [' + queryString + ']}');
}
});

Print ".00" When The Result Is A Whole Number?

I want to try and print ".00" after the variables cache.todayHigh and cache.todayLow are whole numbers.
if (ddimgtooltip.showTips) {
// update tooltip
tip = 'High ' + strings.baro_info + ': ' + cache.todayHigh + ' ' + data.pressunit + ' ' + strings.at + ' ' + data.TpressTH +
' <br> ' + strings.minimum_info + ' ' + strings.baro_info + ': ' + cache.todayLow + ' ' + data.pressunit + ' ' + strings.at + ' ' + data.TpressTL;
if (cache.trendVal !== -9999) {
tip += '<br>' + strings.baro_trend_info + ': ' + baroTrend(cache.trendVal, data.pressunit, true) + ' ' +
(cache.trendValRnd > 0 ? '' : '') + cache.trendValRnd + ' ' + data.pressunit + '/hr';
}
$('#imgtip5_txt').html(tip);
}
e.g. 1017 hPa to 1017.00 hPa.
Is this possible?
Thanks,
William
Try this,
var yvalue = '1702 hpa';
var num = yvalue.replace(/[^0-9]+/ig,"");
value = Number(num).toFixed(2);
var fvalue= value +' '+yvalue.split(' ')[1]
console.log(fvalue);

Error in the Javascript Jquery code

There is an error showing on this code, I have looked through it for hours and cannot find it, could anyone assist please
$(document).ready(function () {
$('#show-records').click(function () {
$.post('./includes/processes.php', function (data) {
var pushedData = jQuery.parseJSON - (DATA);
var htmldata = " ";
$.each(pushedData, function (i, serverData) {
htmldata = htmldata, +'- ' + serverData.Style + ', ' + serverData.Brand + ', ' + serverData.Model + ', ' + serverData.January Registrations + ', ' + serverData.February Registrations + ', ' + serverData.March Registrations + '<br>';
$('#show-list').html(htmldata);
});
});
});
});
htmldata = htmldata, + '
is invalid syntax, try
htmldata = htmldata + '
There is an extra comma after html data on that line. When appending to variables you should try and use this syntax too, enclosing all other strings in "string" .
Change that line of code to:
htmldata += ', - ' + serverDat...
The += is the same as writing htmldata = htmldata + ', -' + .. which is easier to read and maintain.

Remove comma at the begining at the of new results output

I use this script to copy values from span class values in a form. When I copy more than one the script adds a comma at the beginning of the returned results. Not sure what the problem is.
<script>
var results = [];
$('input[name="clickme"]').change(function () {
var id = $(this).attr('class');
$('table#' + id).toggleClass('selected');
var PROC_CODE = $('table#' + id + ' .PROC_CODE').text();
var MEDICARE = $('table#' + id + ' .MEDICARE').text();
var STATUS = $('table#' + id + ' .STATUS').text();
var ATA_ID = $('table#' + id + ' .ATA_ID').text();
var SYS_APP = $('table#' + id + ' .SYS_APP').text();
var SUBMITTER = $('table#' + id + ' .SUBMITTER').text();
var EMAIL = $('table#' + id + ' .EMAIL').text();
var ADD_CPT = $('table#' + id + ' .ADD_CPT').text();
var change = $('table#' + id + ' .change').text();
if ($('input.' + id).is(':checked')) {
results.push(PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n');
if ('console' in window) console.log(results);
$('#results').val(results);
} else {
results.pop(PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n');
$('#results').val(results);
}
});
</script>
var results = [];
You have declared result as an array.
results.push(PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n');
you are pushing elements into array
$('#results').val(results);
results array will be converted into comma seperated string and then it will be put into your #results element
To solve do something like below i.e.
var result=''; //using as string
...
change
results.push(PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n');
to
results= results + ' '+ PROC_CODE + '\n' + SYS_APP + '\n' + change + '\n\n';

Categories