Php 由于parseFloat而获取$NaN且未定义
代码X: 在node.js中运行代码X时,有时会得到$NaN或未定义的值。我怎样才能解决这个问题?因此,cost.php在我的数据库中输入项目的值,同时也以数字格式回传出来,但由于某种原因,parseFloat无法正确读取值。我想在里面加一个if条件Php 由于parseFloat而获取$NaN且未定义,php,mysql,node.js,Php,Mysql,Node.js,代码X: 在node.js中运行代码X时,有时会得到$NaN或未定义的值。我怎样才能解决这个问题?因此,cost.php在我的数据库中输入项目的值,同时也以数字格式回传出来,但由于某种原因,parseFloat无法正确读取值。我想在里面加一个if条件 var url = 'http://'+sitename+'/cost.php?item='+encodeURIComponent(itemname); (function(s
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)
它将wgg[somestuff].cost的结果与$Nan或undefined进行比较,如果结果为true,则执行mysql查询以从数据库中选择价格,或者如果在建议的else中添加分支,则可能不执行parseFloatbody,而直接从数据库中选择项目价格。此外,cost.php在开始时包含此内容,基本上是修改itemname的名称:
else {
wgg[somestuff].cost = parseFloat(body);
}
因此,我可能需要在node.js代码中实现这个新名称,因为数据库中的表具有$item结果格式的项名称,因此如果没有带有%20等的新项名称,我无法执行正确的查询。或者,我可以编辑cost.php,将$item作为初始值存储在数据库中,而不存储%20变成另一个变量?感谢您的任何帮助/建议:
更新-我在一个论坛上发现了这个,我不知道这是否能解决它?如果是,我如何实施?
代码:
那么,当你试图将body解析为float时,它的确切值是多少?它是一个整数。。只是一个数字如果是这样的话,你就得不到NaN了。您应该发布一个导致问题的body值的真实示例。body是由API调用生成的。这有区别吗?不,我要求的是console.dirbody;的输出;。
$item = $_GET['item'];
$item = str_replace("\"", "", $item);
$item = str_replace("\'", "", $item);
$item = str_replace(" ", "%20", $item);
$item = str_replace("\\", "", $item);
(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);
};
}
})();