在Postman中执行算术运算?

时间:2019-04-11 08:15:26

标签: node.js express postman

我正在尝试获取包含使用邮差发送的数值之和的响应。但是,相反,我得到的是串联值。请帮助我。

My Postman screenshot

这是我的代码段:

 const {
            amount,
            old_balance,
            account_number,
            transaction_type,
            cashier
        } = req.body;

        let x, y, sum; 

        x = amount
        y = old_balance; 
        sum = x + y; 

        const newDebit = {

            created_on: create_on,
            transaction_id: transactions.length + 1,
            account_number: parseInt(account_number),
            amount: parseFloat(amount),
            cashier: parseInt(cashier),
            transaction_type: transaction_type,
            old_balance: parseFloat(old_balance),
            new_balance: parseInt(sum)
        }

1 个答案:

答案 0 :(得分:0)

您应该首先将字符串转换为int,然后添加x&y。将代码更改为

const {
    amount,
    old_balance,
    account_number,
    transaction_type,
    cashier
} = req.body;

let x, y, sum; 

x = amount
y = old_balance; 
sum = parseInt(x) + parseInt(y); 

const newDebit = {
    created_on: create_on,
    transaction_id: transactions.length + 1,
    account_number: parseInt(account_number),
    amount: parseFloat(amount),
    cashier: parseInt(cashier),
    transaction_type: transaction_type,
    old_balance: parseFloat(old_balance),
    new_balance: sum
}