Express функции банковского приложения не возвращают правильное значение - PullRequest
0 голосов
/ 24 апреля 2020

Я новичок в express и пытаюсь создать банковское приложение с express, которое создает новый банковский счет и изменяет сумму в счете на основе действий (снятие, депозит).

Мои функции снятия и депозита не работают должным образом - я думаю, это потому, что строка не преобразована в числа, но я не смог выполнить преобразование.

Это мой код:

const express = require('express');
const app = express ();
const port = 3001;

app.listen(port, () => {
    console.log(`server running on port ${port}`)
});

var accounts = [];

app.get('/', (req , res ) => {
    res.send(`<p>Welcome to Caixa bank!</p>`)
});

app.get("/account/new/:accountID/:amount", (req, res) => {
   const { accountID, amount } = req.params;
   var index = accounts.findIndex(account => account == accountID)
    if(index === -1){
        accounts.push({id: accountID, amount: amount})
        res.send(`<p>Account number ${accountID} created with ${amount} Euros</p>`)
    } else {
        res.send(`<p>Account number ${accountID} already exist in database.</p>`)
    }
});


app.get("/:accountID/withdraw/:amount", (req, res) => {
    const { accountID, amount } = req.params;
    var index = accounts.findIndex(account => account.id == accountID)
    if(index !== -1){
        accounts[index].amount -= amount;
        res.send(`<p>${amount} Euros taken from account number ${accountID}</p>`)
    } else {
        res.send(`<p>Account not found.</p>`)
    }
}); 

app.get("/:accountID/deposit/:amount", (req, res) => {
    const { accountID, amount } = req.params;
    var index = accounts.findIndex(account => account.id == accountID)
    if(index !== -1){
        var result =   accounts[index].amount += amount;
        res.send(`<p>${amount} Euros added to account number ${accountID} for a total of ${result}</p>`)
    } else {
        res.send(`<p>Account not found.</p>`)
    }
});

app.get("/:accountID/balance", (req, res) => {
    const { accountID } = req.params;
    var index = accounts.findIndex(account => account.id == accountID)
    if(index !== -1){
        var total =  accounts[index].amount;
        res.send(`<p>The balance of account number ${accountID} is ${total} Euros </p>`)
    } else {
        res.send(`<p>Account not found.</p>`)
    }
});

Большое спасибо за ваш вклад:)

...