MongoServerError:用户无权限执行find操作问题求助
解决MongoServerError: user is not allowed to do action [find] on [GettingStarted.users]错误
我已经为这个错误找了很久的解决方案,试过本站所有相关建议都没能解决。我正在开发一个支持用户注册的网站,需要将用户凭据存储到MongoDB Atlas数据库中。
数据库连接代码
控制台显示“Connected to MongoDB Atlas”,说明连接是成功的:
const { MongoClient } = require("mongodb"); const dbURI = 'mongodb+srv://gettingstarted.owxpopb.mongodb.net/GettingStarted'; mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true }); const db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() { console.log('Connected to MongoDB Atlas'); });
用户Schema定义
const userSchema = new mongoose.Schema({ name: String, email: String, password: String, numberOfTickets: { type: Number, default: 0 } });
数据库操作代码
当执行以下保存用户或检查用户是否存在的代码时:
User.findOne({ email: newUser.email }, (err, existingUser) => { if (err) { // Handle error console.error(err); return res.status(500).send({ message: 'Internal server error' }); } if (existingUser) { // A user with the same email address already exists return res.status(409).send({ message: 'Email address already registered' }); } // If we reach here, it means the user details are not already in the database // You can proceed with saving the user to the database const newUser = new User(userSchema); newUser.save((err, savedUser) => { if (err) { // Handle error console.error(err); return res.status(500).send({ message: 'Internal server error' }); } else{ res.render("home"); } // User saved successfully return res.status(200).send(savedUser); }); });
出现错误:MongoServerError: user is not allowed to do action [find] on [GettingStarted.users]
已尝试的解决方案
- 更新MongoDB安全权限为readWrite
- 设置管理员账户
- 检查Node.js驱动兼容性
以上方法都没有解决问题,恳请大家帮忙。
内容的提问来源于stack exchange,提问作者Lex_X
相关产品推荐
相关产品推荐

