Mongonetworkerror:连接机构被取消
我正在尝试使用Mongoose更新数据库,但是在运行节点应用程序时,我会遇到此网络错误。
const mongoose = require('mongoose')
mongoose.connect("mongodb://localhost:27017/fruitsDB")
const fruitsSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "Why no Name?"]
},
rating: {
type: Number,
min: 1,
max: 10
},
review: String
});
const Fruit = mongoose.model("Fruit", fruitsSchema)
Fruit.find(function(err, fruits){
if(err){
console.log(err)
}
else{
mongoose.connection.close();
fruits.forEach(function(fruit){
console.log(fruit.name)
})
}
})
Fruit.updateOne({_id:"62b6a681eb136efde7ed17bc"}, {name: "Banana"}, function(err){
if(err){
console.log(err)
}
else{
console.log("Successfully updated the document")
}
})
错误:运行节点应用程序时,Commnad Line错误
MongoNetworkError: connection establishment was cancelled
at connectionFailureError
at CancellationToken.<anonymous>
at Object.onceWrapper (node:events:641:28)
at CancellationToken.emit (node:events:527:28)
at ConnectionPool.close
at Server.destroy
at destroyServer
at eachAsync
是使用Mongoose创建的简单节点应用程序。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您扭曲
mongoose.connection.close()
在超时,则不会发生错误。我认为这是因为连接尚未完成,但尚未完成操作。我尝试将其设置为较低的数字,并遇到相同的错误,但是5很好。如果您有很多事情要做,则可能需要设置更大的数字。
If you warp
mongoose.connection.close()
in a timeout the error won't happen. I think its because the connection is getting closed while the operations are not done yet.I tried setting it to a lower number and got the same error, but 5 is fine. You may need to set a bigger number if you have a lot going on.
Amy和Pineapple☑️错误线:MongonetWorkerror:连接机构在ConnectionFailureError 取消了
您只需要保存菠萝和人 最后 对我有用。
☑️Solution to the establishing relationship problem between Amy and Pineapple☑️ ERROR Line: MongoNetworkError: connection establishment was cancelled at connectionFailureError
You just need to save both the pineapple and the person together in the end and it worked for me.
呼叫查找功能上次对我有用。我的意思是,这样 -
关闭连接应结束,这就是为什么代码无法正确执行的原因。
Calling the find function last worked for me. I mean, like this -
Closing the connection should be at end which is the reason why the code is not getting properly executed.
我尝试将查找功能持续调用,但仍会遇到这样的错误:
我不知道该问题在运行应用程序中发生时如何解决该问题,但是目前,如果您只想将文档插入集合中。访问方法,然后运行应用程序将成功插入该应用程序,然后评论
.updateOne
方法和删除。我做了相同的!
,否则
我发现出于某种原因。输入方法在
.updateone
之前被执行,因此在收集更新之前,连接已关闭。因此,如果我们这样做它有效。
I tried Calling the find function last but still getting the same error like this:
I don't know how to tackle this issue when it occurs in a running application but for now if you want to just insert the docs in collection then just comment the .find method completely and then run the application it will be inserted successfully and then comment the
.updateOne
method and uncomment the .find method by doing you will be successfully added the docs and could get the find result.I did the same!
OR
I found out that for some reason .find method gets executed before
.updateOne
so the connection were being closed before the collection gets updated.So, if we do this it works.
您无法关闭查找方法中的连接。关闭连接后,您无法与DB进行交互。只需将
mongoose.connection.close()
放在最后即可。You can't close the connection in the find method. You are not able to interact with the db after you closed the connection. Just put
mongoose.connection.close()
at the end.只需做这样的事情:
关闭
insertmany内部的连接
对我有用Just Do something like this:
Closing the connection inside
insertmany
worked for me阅读了一些解决方案后TL:DR是:
mongoose.disconnect();
,这是引起问题的一种After reading some solutions the TL:DR is: the
mongoose.disconnect();
, it's the one causing problems, the connection is being terminated before you can update anything在数据库上使用CRUD方法时,应该要小心。因为这些方法是异步的。
在您的情况下,
查找
方法先执行并在更新
方法之前关闭数据库连接。解决方案:您可以通过简单地更改代码中的逻辑(例如嵌入 find 方法
update> updateOne
方法方法或根据您的需要)来解决该问题。(通过嵌入我们正在做一种方法按顺序执行它们)You should be careful while using crud methods on database. Because those methods are asynchronous.
In your case the
find
method executed first and closed the database connection prior toupdateOne
method.Solution: You can solve that by simply changing the logic in your code like embedding find method inside
updateOne
method or viseversa according to your need.(By embedding we are making a way to execute them in order)