node.js 中的 NaN 替换为整数
NaN replacement in node.js with an integer
我正在使用以下代码更新 node.js
中的数据库
var mBooking = rows[0];
var distance = mBooking.distanceTravelled;
var lastLng = mBooking.lastLng;
var lastLat = mBooking.lastLat;
if(lastLat == 0)
{
lastLat = lat;
lastLng = lng;
}
var currentPoint = new GeoPoint(lat, lng);
var oldPoint = new GeoPoint(lastLat, lastLng);
distance = distance + (currentPoint.distanceTo(oldPoint, true) * 1000);
if(distance == null)
distance = 0;
var query = "UPDATE bookings SET lastLat = " + lat + ", lastLng = " + lng + ", distanceTravelled = " + distance + " WHERE id = " + mBooking.id;
console.log(query);
这是我的控制台查询
UPDATE bookings SET lastLat = 25.0979065, lastLng = 55.1634082, distanceTravelled = NaN WHERE id = 43
我如何检查距离是否为 NaN 然后我可以用 0 替换它。
现在,如果我尝试更新它会出现数据库错误
使用isNaN()
.
if (isNaN(distance))
distance = 0;
您也可以使用 inline-if 将其压缩为一行:
distance = (isNaN(distance) ? 0 : distance);
我正在使用以下代码更新 node.js
中的数据库var mBooking = rows[0];
var distance = mBooking.distanceTravelled;
var lastLng = mBooking.lastLng;
var lastLat = mBooking.lastLat;
if(lastLat == 0)
{
lastLat = lat;
lastLng = lng;
}
var currentPoint = new GeoPoint(lat, lng);
var oldPoint = new GeoPoint(lastLat, lastLng);
distance = distance + (currentPoint.distanceTo(oldPoint, true) * 1000);
if(distance == null)
distance = 0;
var query = "UPDATE bookings SET lastLat = " + lat + ", lastLng = " + lng + ", distanceTravelled = " + distance + " WHERE id = " + mBooking.id;
console.log(query);
这是我的控制台查询
UPDATE bookings SET lastLat = 25.0979065, lastLng = 55.1634082, distanceTravelled = NaN WHERE id = 43
我如何检查距离是否为 NaN 然后我可以用 0 替换它。
现在,如果我尝试更新它会出现数据库错误
使用isNaN()
.
if (isNaN(distance))
distance = 0;
您也可以使用 inline-if 将其压缩为一行:
distance = (isNaN(distance) ? 0 : distance);