!当我由特定用户创建博客时,发生与 mongoServer 相关的错误。 !这是我的创建博客 API代码:
//Create blog....
const creatBlogs = async(req, res)=>{
const {title, description, image,user} = req.body
try {
let existingUser = await userModel.findById(user)
console.log(existingUser);
if(!existingUser){
return res.status(404).json({message:"Unable to find User by this id"})
}
if(!title || !description){
return res.status(400).json({message:"Missing some fields"})
}
const newBlog = new Blog({
title,
description,
image,
user
});
console.log(newBlog);
const session = await mongoose.startSession();
session.startTransaction();
await newBlog.save({session})
existingUser.blogs.push(newBlog)
await existingUser.save({session})
await session.commitTransaction();
return res.status(201).json({newBlog:newBlog})
} catch (error) {
console.log("Error occured", error);
}
}
输出:发生错误 MongoServerError:事务号仅允许在副本集成员或 mongos 上
我假设您在不允许事务的独立 mongo 服务器上运行应用程序。您应该配置副本集或分片集群来处理事务。请参阅此处