【发布时间】:2022-09-29 02:55:16
【问题描述】:
我正在尝试使用 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\")
}
})
错误:运行节点应用程序时出现命令行错误
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 创建的简单 Node 应用程序。
-
类似问题已回答here
标签: javascript node.js mongodb mongoose