Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having Challenges with Node.js Update SQL for Arithemetics

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading