简体   繁体   English

“错误:MongooseError:操作 `users.insertOne()` 缓冲在 10000 毫秒后超时”,

[英]"Error: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms",

I'm running MongoDB Atlas on node express and I got this error when I tested with postman.我在 node express 上运行 MongoDB Atlas,当我用 postman 测试时出现了这个错误。

const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');

require('dotenv').config();

const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true }
);
const connection = mongoose.connection;
connection.once('open', () => {
  console.log("MongoDB database connection established successfully");
})

const exercisesRouter = require('./routes/exercises');
const usersRouter = require('./routes/users');

app.use('/exercises', exercisesRouter);
app.use('/users', usersRouter);

app.listen(port, () => {
    console.log(`Server is running on port: ${port}`);
});

This is my .env , I'm guessing the problem might be here too, Kindly help:这是我的.env ,我猜问题也可能在这里,请帮忙:

ATLAS_URI=mongodb+srv://userone:useronepassword1234@cluster0.swye5.mongodb.net/<dbname>?retryWrites=true&w=majority

In my case, I had to go to Atlas, and reset my whitelisted IP to the correct address.就我而言,我必须将 go 设置为 Atlas,并将我列入白名单的 IP 重置为正确的地址。

Then I restarted my local server and tried posting again on postman... And it worked!然后我重新启动了我的本地服务器并尝试在 postman 上再次发布......它成功了!

  1. First replace <dbname> with your actual DB name, if not created, create one.首先将<dbname>替换为您的实际数据库名称,如果未创建,请创建一个。

  2. Then create collection as required on the Atlas UI itself.然后根据需要在 Atlas UI 上创建集合。

  3. In the Network Access, click on ADD IP ADDRESS and select " allow access from anywhere ".在网络访问中,单击添加 IP ADDRESS 和 select“允许从任何地方访问”。

  4. Rewrite your code this way:用这种方式重写你的代码:

mongoose 
 .connect(process.env.MONGO_PROD_URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,   })   
 .then(() => console.log("Database connected!"))
 .catch(err => console.log(err));
  1. Now your DB should be connected and working fine.现在您的数据库应该已连接并且工作正常。

If still not resolved, check this link .如果仍未解决,请检查此链接

I was facing the same issue.我面临着同样的问题。 It is resolved.解决了。 I think you might have not allowed network access to everyone in Atlas: MongoDB.我认为您可能不允许 Atlas 中的每个人都可以访问网络:MongoDB。 Do it will resolve the issue.这样做会解决问题。

Check your Network Access IP list in MongoDB Cloud.You will only be able to connect to your cluster from the list of IP Addresses在 MongoDB 云中检查您的网络访问 IP 列表。您将只能从 IP 地址列表连接到您的集群

Ensure that your IP Address Setting is Active确保您的 IP 地址设置处于活动状态

Check if you didn't forgot to set password in connection string.检查您是否忘记在连接字符串中设置密码。

Step 1:步骤1:

Go to your Atlas account and open your project. Go 到您的 Atlas 帐户并打开您的项目。

Step 2:第2步:

In the left menu, navigate to Network Access section:在左侧菜单中,导航到Network Access部分:

在此处输入图像描述

Step 3:第 3 步:

Add your IP Address so only you would be able to connect to your cluster.添加您的 IP 地址,以便只有您能够连接到您的集群。 You can also add 0.0.0.0/0 and it will allow access from everywhere.您还可以添加0.0.0.0/0 ,它将允许从任何地方访问。

I also faced the same error.我也面临同样的错误。 In my case, the error was coming up because useFindAndModify was set to false in mongoose connection就我而言,出现错误是因为在 mongoose 连接useFindAndModify设置为false

Code with error有错误的代码

mongoose.connect(dbUrl, { 
    useNewUrlParser: true, 
    useUnifiedTopology: true, 
    useFindAndModify: false 
}, () => { 
    console.log('connected to database myDb ;)') 
})

Working Code工作代码

mongoose.connect(dbUrl, { 
    useNewUrlParser: true, 
    useUnifiedTopology: true
}, () => { 
    console.log('connected to database myDb ;)') 
})

If you are seeing this using Typescript ensure you are importing the connect function from mongoose and use that to connect.如果您使用 Typescript 看到此内容,请确保您从 mongoose 导入连接 function 并使用它进行连接。

import { connect } from "mongoose";

connect(...).then()

It's kind of late but probably because of this line useCreateIndex: true it's not working.有点晚了,但可能是因为这条线useCreateIndex: true它不起作用。 It seems in mongoDB version 5. this is not supported anymore.似乎在 mongoDB 版本 5 中。不再支持此功能。 Rewrite like the answer of manoj_mi5像 manoj_mi5 的答案一样重写

mongoose
  .connect(process.env.MONGODB_URL, {
      useNewUrlParser: true,
      useUnifiedTopology: true})
  .then(() => console.log("Database connected!"))
  .catch(err => console.log(err));

to check if there are more errors.检查是否有更多错误。

Change your MongoDB database-user password.更改您的 MongoDB 数据库用户密码。 It should be alphanumeric with no special characters.它应该是没有特殊字符的字母数字。 Nothing worked out for me, but, changing the password did.对我来说没有任何效果,但是更改密码确实有效。

"error:MongooseError: Operation users.insertOne() buffering timed out after 10000ms" “错误:MongooseError:操作users.insertOne()缓冲在 10000 毫秒后超时”

 mongoose.connect(process.env.MONGO_URL, { useNewURLParser: true, useUnifiedTopology: true, useCreateIndex: true, },6000000).then(console.log("connected to server")).catch((err) => console.log(err));

add time like 6000000 after options在选项后添加时间,例如 6000000

In Mongoose version 6 and above don't require those在 Mongoose 版本 6 及更高版本中不需要这些

{    useNewUrlParser: true, 
     useUnifiedTopology: true, 
     useCreateIndex: true, 
     useFindAndModify: false
}

so just delete it.所以只需删除它。 And if you still see app crashed - waiting for file changes before starting... just save it one more time and it will work如果您仍然看到app crashed - waiting for file changes before starting...只需再保存一次,它就会工作

In Latest version of mongoose.在 mongoose 的最新版本中。

we don't require this object.我们不需要这个 object。

{ useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false } { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false }

But if you are dealing with older versions of mongoose then definetly you need it.但是,如果您正在处理旧版本的 mongoose,那么您肯定需要它。 Also in your mongodb network address add this address 0.0.0.0/0 in place of your ip address.同样在您的 mongodb 网络地址中添加此地址 0.0.0.0/0 代替您的 ip 地址。

in this line of code, ATLAS_URI=mongodb+srv://userone:useronepassword1234@cluster0.swye5.mongodb.net/?retryWrites=true&w=majority在这行代码中,ATLAS_URI=mongodb+srv://userone:useronepassword1234@cluster0.swye5.mongodb.net/?retryWrites=true&w=majority

make sure you write actual database name without < > symbols.确保您写的实际数据库名称不带 < > 符号。 You have to create your database first in Mongo Atlas.您必须首先在 Mongo Atlas 中创建数据库。 在此处输入图像描述

To solve this, I created a function in index.js, where I asynchronically connecting to my Database and then starting the server, because mongoose doesn't wait for db connection it executes everything on spot, for me that was the problem.为了解决这个问题,我在 index.js 中创建了一个 function,我在其中异步连接到我的数据库,然后启动服务器,因为 mongoose 不等待数据库连接它就地执行所有操作,对我来说这就是问题所在。

async function start() {
  try {
    //Database Connect
    await mongoose.connect(
      process.env.DB_CONNECTION,
      {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      },
      () => {
        console.log("Database Connected");
      }
    );

    app.listen(3000, () => {
      console.log("Server is running on port 3000 ...");
    });
  } catch (error) {
    console.error(error);
  }
}

Double-check some of the things listed below.仔细检查下面列出的一些内容。

  1. Check the username in the database and make sure you have used the same username in the application.检查数据库中的用户名并确保您在应用程序中使用了相同的用户名。
  2. Check the password.检查密码。
  3. Check the URl of the Database (if any letter is different or changed).检查数据库的 URl(如果任何字母不同或更改)。
  4. Check the network access if your IP is not present in the IP Access list of the network access section.如果网络访问部分的 IP 访问列表中不存在您的 IP,请检查网络访问。 (Add your IP address if not present there or create add a new IP address). (如果那里不存在,请添加您的 IP 地址或创建添加新的 IP 地址)。

Add a new IP Address by: -> click on ADD IP ADDRESS -> click ALLOW ACCESS FROM ANYWHERE -> click confirm.通过以下方式添加新的 IP 地址:-> 点击添加 IP 地址 -> 点击允许从任何地方访问 -> 点击确认。

  1. Also check, if you are on your company's network and added that IP Address to the IP Access list, you might face the same issue, if so, then try switching to your mobile internet or some other than the company's network.还要检查,如果您在您公司的网络上并将 IP 地址添加到 IP 访问列表中,您可能会遇到同样的问题,如果是这样,请尝试切换到您的移动互联网或公司网络以外的其他网络。

Now, run the application.现在,运行应用程序。

Checking the above-all points and making them correct has fixed the issue for me.检查以上所有要点并使其正确已为我解决了问题。 Hope the same for you.希望你也一样。 :) :)

Mention Your database name dbname -> yourDbName without < > this will solve your problem提及您的数据库名称 dbname -> yourDbName without < > 这将解决您的问题

URI=mongodb+srv://<username>: 
<password>@cluster0.swye5.mongodb.net/<dbname>?retryWrites=true&w=majority

I had this similar issue of recent and what I think gave the error was我最近遇到了类似的问题,我认为错误是

require('dotenv').config()需要('dotenv').config()

Changed it to this改成这个

require('dotenv/config')需要('dotenv/config')

or或者

require('dotenv')需要('dotenv')

after importing the package, below call the config function导入 package 后,调用配置 function

dotenv.config() dotenv.config()

This error can be caused by typos in user properties.此错误可能是由用户属性中的拼写错误引起的。 (I was trying to set "Email" instead of what was defined on my user model: "email"). (我试图设置“电子邮件”而不是在我的用户 model 上定义的内容:“电子邮件”)。

I experienced the same issue.我遇到了同样的问题。 But my MongoDB is running locally on my machine.但是我的 MongoDB 在我的机器上本地运行。 I had forgotten to open the connection before sending my query to the database.在将查询发送到数据库之前,我忘记打开连接。 So, I added the code to open and close the connection, and it worked.所以,我添加了打开和关闭连接的代码,它工作了。

 try{ await mongoose.connect(uri); // My mongoose database request code } finally{ await mongoose.connection.close(); }

I had the same issue, I removed useCreateIndex: true , and used only:我遇到了同样的问题,我删除useCreateIndex: true ,并且只使用了:

{
  useNewUrlParser: true
}

So I faced the same error but for my case, the reason was because I used the mongoose.createConnection(...) method instead of the mongoose.connect(...) method.所以我遇到了同样的错误,但就我而言,原因是因为我使用了mongoose.createConnection(...)方法而不是mongoose.connect(...)方法。

The relevant difference between both of them is that, with mongoose.connect , the created connection is automatically linked with your mongoose models when you do mongoose.model('User', userSchema) .两者之间的相关区别在于,使用mongoose.connect时,创建的连接会在您执行mongoose.model('User', userSchema)时自动链接到您的猫鼬模型。 However, with mongoose.createConnection , you need to link it with your schema directly like so:但是,对于mongoose.createConnection ,您需要像这样直接将它与您的模式链接起来:

import * as mongoose from 'mongoose'
import { userSchema } from '../path/to/your/schema'

const dbURL = 'mongodb://localhost:27017'
const db = mongoose.createConnection(dbURL, {dbName: 'my-db-name'})

export const User = db.model('User', userSchema)

The important bit is that on the last line, we use the created connection instance db to create our model, rather than using mongoose.model directly.重要的一点是在最后一行,我们使用创建的连接实例db来创建我们的模型,而不是直接使用mongoose.model

Keep in mind, this solution is only relevant when you use mongoose.createConnection instead of mongoose.connect请记住,此解决方案仅在您使用mongoose.createConnection而不是mongoose.connect时才相关

Creating New database user Worked for me.创建新的数据库用户为我工作。

I turned off my mobile hotspot and back on and it worked.我关闭了我的移动热点并重新打开并且它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 操作 `users.insertOne()` 缓冲在 10000 毫秒后超时,Node.js,MongooseError - Operation `users.insertOne()` buffering timed out after 10000ms in, Node.js, MongooseError MongooseError:操作 `users.insertOne()` 缓冲在 10000 毫秒后超时 - MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms MongooseError:操作 .insertOne() 缓冲在 10000 毫秒后超时 - MongooseError: Operation `.insertOne()` buffering timed out after 10000ms mongooseError:操作 `users.insertOne()` 缓冲在 10000 毫秒后超时 - mongooseError: Operation `users.insertOne()` buffering timed out after 10000 ms MongooseError:操作“fruits.insertOne()”缓冲在 10000 毫秒后超时 - MongooseError: Operation 'fruits.insertOne()' buffering timed out after 10000ms 来自 MongoDB 的错误消息“操作 `disneys.insertOne()` 缓冲在 10000 毫秒后超时”” - Error Message from MongoDB "Operation `disneys.insertOne()` buffering timed out after 10000ms"" MongoDB insertOne() 缓冲在 10000 毫秒后超时 - MongoDB insertOne() buffering timed out after 10000ms “消息”:“errorMongooseError:操作 `userinfos.insertOne()` 缓冲在 10000 毫秒后超时” - "message": "errorMongooseError: Operation `userinfos.insertOne()` buffering timed out after 10000ms" mongooseError:操作 `chatbots.findOne()` 缓冲在超时 10000 毫秒后超时。<anonymous></anonymous> - mongooseError: Operation `chatbots.findOne()` buffering timed out after 10000ms at Timeout.<anonymous> MongooseError:操作“featureds.find()”缓冲在 10000 毫秒后超时 - MongooseError: Operation 'featureds.find()` buffering timed out after 10000ms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM