CODE X:
var url = 'http://'+sitename+'/cost.php?item='+encodeURIComponent(itemname);
(function(somestuff) {
request(url, function(error, response, body){
if(!error && response.statusCode === 200){
var unixtime = Math.round(new Date().getTime()/1000.0);
if(body == "notfound") { offers.declineOffer({tradeOfferId: offer.tradeofferid}); mysqlConnection.query('INSERT INTO `messages` (`userid`,`msg`,`from`, `win`, `system`, `time`) VALUES (\''+offer.steamid_other+'\',\'Item not available \',\'System\', \'0\', \'1\', \''+unixtime+'\')', function(err, row, fields) {}); }
else {
wgg[somestuff].cost = parseFloat(body);
}
} else offers.declineOffer({tradeOfferId: offer.tradeofferid});
});})(i)
I am getting $NaN sometimes or undefined values when running CODE X in node.js. How can I solve this? So, the cost.php inputs the value of the items in my database while also echoing it out in number format, but parseFloat doesn't read the value properly for some reason. I was thinking of adding an "if" condition inside
else {
wgg[somestuff].cost = parseFloat(body);
}
which compares the result of wgg[somestuff].cost with $Nan or undefined and if result is true then do mysql query to select the price from the database or maybe instead of doing parseFloat(body) just select the item price from database directly (no more "if" branch added within the suggested else. Also, cost.php contains this at the beginning which is basically modifying the name of "itemname":
$item = $_GET['item'];
$item = str_replace("\"", "", $item);
$item = str_replace("\'", "", $item);
$item = str_replace(" ", "%20", $item);
$item = str_replace("\\", "", $item);
So probably I need to implement this new name into the node.js code because the table in the database has the name of the item in the format of the $item result so I can't execute a proper query without having the new item name (with%20 etc). Or maybe I can edit cost.php to store $item in the database as initial value without %20 by memorizing it into another variable? Any help/suggestions appreciated :).
UPDATE-- I found this on a forum, and I don't know if this would fix it? If yes, how can I implement it? CODE:
(function() {
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// Decimal round
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
// Decimal floor
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
// Decimal ceil
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();