Node.js+PostgreSQL查询返回数据在EJS中偶现NaN值的问题求助
Node.js+PostgreSQL查询返回数据在EJS中偶现NaN值的问题求助
我现在遇到一个头疼的问题:在EJS页面展示item.total_amount、item.price、item.addtional_price这些字段时,偶尔会得到NaN值。调试的时候发现这些变量有时候有值,有时候又没有,完全摸不准规律。
我的Node.js查询和路由代码:
const getCustomers = async (req) => { const name = req.query.name || null; try { let query = ` SELECT customers.id AS customer_id, customers.name AS customer_name, customers.phone AS customer_phone, SUM(receipt_vouchers.amount) AS total_amount, reservations.price, reservations.addtional_price FROM customers LEFT JOIN receipt_vouchers ON receipt_vouchers.customer_id = customers.id LEFT JOIN reservations ON reservations.customer_id = customers.id `; let params = []; // If a name is provided, add a WHERE clause to filter by customer name if (name) { query += ` WHERE customers.name ILIKE $1`; params.push(`%${name}%`); } // Group by customer and reservation columns to avoid aggregation on reservations values query += ` GROUP BY customers.id, customers.name, customers.phone, reservations.price, reservations.addtional_price `; // Execute the query const result = await db.query(query, params); return result.rows; } catch (error) { console.error("Error fetching customers:", error); throw error; } }; app.get("/customers", async (req, res) => { try { const customers = await getCustomers(req); console.log(customers.map(row => row)); res.render("customers.ejs", { // Ensure the template path is correct data: customers }); } catch (error) { console.error("Error in /customers route:", error); // Debugging res.status(500).json({ error: "Internal Server Error" }); // Handle errors } });
对应的EJS模板代码片段:
<tbody id="tableBody"> <% if (data.length > 0) { %> <% data.forEach(function(item) { console.log("total_amount:", item.total_amount); console.log("price:", item.price); console.log("additional_price:", item.additional_price); let theAmount = parseInt(item.total_amount); let thePrice = parseInt(item.price) ; let theAdditionalPrice = parseInt(item.additional_price); let total = thePrice + theAdditionalPrice; let balance = total - theAmount; %> <tr> <td><%= item.customer_id %></td> <td><%= item.customer_name %></td> <td><%= item.customer_phone %></td> <td><%= item.total_amount %></td> <td><%= total %></td> <td><%= balance %></td>
调试时的控制台输出:
total_amount: null price: null additional_price: undefined total_amount: null price: null additional_price: undefined total_amount: 322523.00 price: 2938429 additional_price: undefined total_amount: null price: null additional_price: undefined total_amount: 3999.00 price: null additional_price: undefined total_amount: 83834.00 price: 39382 additional_price: undefined total_amount: 2000.00 price: null additional_price: undefined total_amount: 10000.00 price: 100000 additional_price: undefined total_amount: null price: null additional_price: undefined
可以看到有的行里total_amount有值但price是null,有的行price有值但additional_price是undefined,还有的行三个值全是空的,这直接导致我用parseInt转换后得到NaN,进而让total和balance的计算结果也出错。
我尝试过的方法:
- 把数据库返回的值用
parseInt和parseFloat转换,但还是会出现NaN; - 咨询过AI,它建议用
parseFloat(item.additional_price)||0这种方式,但这会把真实的缺失值变成0,得到错误的计算结果——我需要的是数据库里的真实值,而不是默认填充0。
有没有大佬能帮我分析下为什么会出现这种偶尔有值偶尔没值的情况?应该怎么修改查询或者前端处理逻辑,才能正确拿到真实数据,避免NaN的同时又不篡改真实的缺失状态?
备注:内容来源于stack exchange,提问作者Ali Alariqi
相关产品推荐
相关产品推荐

