我想在Node.JS中执行此操作
我希望有这样的链接,它应该是一个GET请求
本地主机:3000/api/bal/update-balance?email=aa@email.com&金额=4500
如何在Node.JS中执行此操作
我的代码看起来像这样
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 });
}
});
模块.导出=路由器;
请参见上述代码。
1条答案
按热度按时间aamkag611#