How can I pass multiple Parameters in Node.JS with &

I want to do this in Node.JS

I want to have a link like this, it should be a GET Request

http://localhost:3000/api/bal/update-balance?email=aa@email.com&amount=4500

How can I do this in Node.JS

My code is looking Thus

const router = require("express").Router();
const User = require("../models/User");
const Trans = require("../models/Transactions");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");


router.post("/update-balance", async (req, res) => {

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

        const amount = parseInt(req.body.amount);

        const user = await User.find({ email: req.body.email });
        const balance = parseInt(user[0].balance);

        //return balance;
        //console.log(balance);

        const total_amt = amount + balance;
        //console.log(total_amt);

        // update Balance
        //const wallet_user = new User();
        try{
          await User.findOneAndUpdate({email : req.body.email}, {$set: {balance: total_amt}});
          const transactions = new Trans({
            email: req.body.email,
            narration: 'NEW DEPOSIT - '+amount,
            credit: amount,
            debit: 0.00,
            amount: amount,
          });
          transactions.save();
        }catch(err){
          console.log(err);
        }

        return res.send({ error: false, message: "OK" });

      } catch (error) {
        res.status(404).json({ message: error.message });
      }

});

module.exports = router;

Please See above Code.

>Solution :


router.get(("/update-balance"), async( req,res) => {
   try {
      const amount = req.query.amount;
      const email = req.query.email;   
      //you can do the rest of the work here

    }
    catch (error) {
        res.status(500).json({ message: error.message })
    }
})

Leave a Reply