我遇到此错误
2月20日06:51:27 AM GET/api/wallet/www.example.com &金额= 16800 200 702.827毫秒-2月20日52日06:51:27 AM(节点:52)未处理承诺拒绝警告:wallet-transfer?email=john_kross2@yopmail.com事务验证失败:email=ng1@gmail.com&amount=16800 200 702.827 ms - 52 Feb 20 06:51:27 AM (node:52) UnhandledPromiseRejectionWarning: ValidationError: Transactions validation failed: email: Cast to string failed for value "[ '***@yopmail.com', '***@gmail.com' ]" (type Array) at path "email"
但检查 Postman 它告诉我,一切都很好,但当我检查数据库,我没有看到任何工作正常。
我的端点应该把钱从一个钱包发送到另一个钱包(虚拟的),但这似乎不起作用,不知道为什么。
https://*********.onrender.com/api/wallet/wallet-transfer?email=**@yopmail.com&email=**@gmail.com&amount=16800
我的代码
const router = require("express").Router();
const User = require("../models/User");
const Trans = require("../models/Transactions");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
router.get("/wallet-transfer", 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.query.amount);
//Debit user
const debit = await User.find({ email: req.query.email });
const debit_balance = parseInt(debit[0].balance);
const debit_amt = debit_balance - amount;
//credit User
const credit = await User.find({ email: req.query.email });
const credit_balance = parseInt(credit[0].balance);
const credit_amt = credit_balance + amount;
//set Debit account with updated amount
try{
await User.findOneAndUpdate({email : req.query.email}, {$set: {balance: debit_amt}});
const transactions = new Trans({
email: req.query.email,
narration: 'DEBIT - '+debit_amt,
credit: 0.00,
debit: debit_amt,
amount: debit_amt,
});
transactions.save();
//set credit account with updated amount
await User.findOneAndUpdate({email : req.query.email}, {$set: {balance: credit_amt}});
const trx = new Trans({
email: req.query.email,
narration: 'CREDIT - '+credit_amt,
credit: credit_amt,
debit: 0.00,
amount: credit_amt,
});
trx.save();
}catch(err){
console.log(err);
}
return res.send({ error: false, message: "Wallet Transfer Complete" });
} catch (error) {
res.status(404).json({ message: error.message });
}
});
module.exports = router;
如何解决呢?
1条答案
按热度按时间2vuwiymt1#
好的,我让它工作了。MongoDB和NodeIdeJS是非常严格的。第二个参数需要从email改为email2,它和我预期的一样工作。相同的源代码。
谢谢大家。