Home > Software design >  Having Challenges with Node.js Update SQL for Arithemetics
Having Challenges with Node.js Update SQL for Arithemetics

Time:05-19

When I do this

update fasta_users set balance = balance 7000 where id =1;

it works fine, No challenge, but when i want to work on it programmatically, it tells me amount has to be a column, i do not understand why it behaves so.

My code looks thus :

app.put('/api/v1/user/fundWallet',async function(req,res,next){
    try {

        if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ') || !req.headers.authorization.split(' ')[1]) {
            return res.status(422).json({ message: 'Please Provide Token!' })
        }

        var id = req.body.id;
        const amount = req.body.amount;
        if (!id) {
            return res.status(400).send({ error: true, message: 'Please provide ID' });
        }
        dbConn.query(`update fasta_users set balance = balance   amount=${amount} where id =${id}`,function (error, results, fields) {
            if (error) throw error;
            return res.send({ error: false, data: results, message: 'users List.' });
        });

    } catch (err) {
        next(err);
    }
})

I do not understand why it behaves so. Please I do need guidance.Or Do I have to use a Stored procedure here?

CodePudding user response:

Your code does not produce the same SQL as your test - it sends

update fasta_users set balance = balance   amount=7000 where id =1

(assuming the same values for amount and id as in your question).

Getting rid of the amount= should work.

  • Related