我正在运行多个数据库操作。当其中之一失败时,我想恢复运行的操作。因此,我使用来自 Mongoose 的 交易会话。
我正在尝试确定交易是“已发送”还是“已收到”。但我将所有交易标记为“已收到”。我尝试借助日志进行检查,但只有 userId 日志在工作,尽管余额已更新并且交易正在完成。 我认为日志系统或异步性质存在一些问题。
router.post("/transfer", authMiddleware, async (req, res) => {
try {
// Log userId
console.log("userId:", req.userId);
const session = await mongoose.startSession();
session.startTransaction();
const { amount, to } = req.body;
console.log("Amount to transfer:", amount);
console.log("Recipient:", to);
//fetch the accounts within the transaction
const account = await Account.findOne({ userId: req.userId }).session(
session
);
const toAccount = await Account.findOne({ userId: to }).session(session);
console.log("Sender account found with userId:", account.userId);
console.log("Receiver account found with userId:", toAccount.userId);
if (!account || account.balance < amount) {
await session.abortTransaction();
return res.status(400).json({ message: "Insufficient balance" });
}
if (!toAccount) {
await session.abortTransaction();
return res.json({ message: "Receiver account not found" });
}
//perform the transfer
await Account.updateOne(
{ userId: req.userId },
{ $inc: { balance: -amount } }
).session(session);
await Account.updateOne(
{ userId: to },
{ $inc: { balance: amount } }
).session(session);
//Determine the transaction type(sent/received)
const transactionType = req.userId === account.userId ? "sent" : "received";
console.log("transaction type:", transactionType);
//save the transaction
const transaction = new Transaction({
senderId: req.userId,
receiverId: to,
amount: amount,
type: transactionType,
});
await transaction.save({ session: session });
//commit the transaction
await session.commitTransaction();
res.json({ message: "Transfer successful" });
} catch (error) {
console.error("Error transferring funds:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
const jwt = require("jsonwebtoken");
const { JWT_SECRET } = require("../config.js");
const authMiddleware = (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
// console.log("Unauthorized: No Bearer token found in headers");
return res.status(403).json({ message: "Unauthorized" });
}
const token = authHeader.split(" ")[1];
try {
const decoded = jwt.verify(token, JWT_SECRET);
req.userId = decoded.userId;
// console.log("User authenticated. User ID:", req.userId);
next();
} catch (error) {
console.error("Error verifying token:", error.message);
return res.status(403).json({ message: "Unauthorized" });
}
};
module.exports = authMiddleware;
请检查下面的代码。该路由处理程序是一个异步处理程序,因此需要注意其错误处理。
从express 4开始,异步处理程序中的错误需要显式传递给express。 捕获错误
router.post("/transfer", authMiddleware, async (req, res) => {
try {
...
...
...
} catch (error) {
console.error("Error transferring funds:", error);
res.status(500).json({ message: "Internal Server Error" });
}
试试这个方法:
router.post("/transfer", authMiddleware, async (req, res) => {
try {
...
...
...
} catch (error) {
next(error);
}
它将捕获错误并将其路由到应用程序级别的错误处理程序。
// error handler
app.use((err, req, res, next) => {
res.status(400).send(err.message);
});
我还没有浏览你的整个代码,但这些对于你继续调试这段代码仍然是必要的。