Mongoose在Model.InsertMany()上超时;数据库没有出现在MongoDB shell



我有节点v14.17.6, MongoDB 5.0和mongoose 5.13.8(旧版本)。我的app.js:

//0. setup
const express = require("express");
//require module date.js, which binds exports to const date
// const date = require(__dirname + "/date.js");
//console.log(date()); what i exported from the date.js module
const app = express();
const mongoose = require("mongoose");
app.use(express.urlencoded({extended: true})); //alt to body parser
app.use(express.static("public")); //static files are in the "public" folder
app.set('view engine', 'ejs'); //set app view engine to EJS
//use mongo and mongoose instead of arrays to store data
//a) connect to mongoDB
mongoose.connect("mongodb://localhost:27107/todolistDB", {useNewUrlParser: true, useUnifiedTopology: true});
//b) create schema
const itemsSchema = new mongoose.Schema ({
name: String
});
//c) create model
const Item = mongoose.model('item', itemsSchema);
//d) create documents
const item1 = new Item({
name: "wake up (8 am)"
});
const item2 = new Item({
name: "cry (8:30 am - 12 pm)"
});
const item3 = new Item({
name: "listen to “sexyback” on loop (12 pm - 7 pm)"
});
const item4 = new Item({
name: "punch a hole in the wall and patch it up (7 pm - 7:30 pm)"
});
const item5 = new Item({
name: "sleep (8 pm)"
});
//e) put docs into array
const defaultItems = [item1, item2, item3, item4, item5];
//f) instert items into collection
Item.insertMany(defaultItems, function(e){
if (e) {
console.log(e);
} else {
console.log("congration you done it");
}
});
//1. regular list
app.get("/", function(req,res) {
//we call function that's bound to const date and we activate getDate()
// const day = date.getDate(); //variable day is bound to the output of the date module
//render file called views/list.ejs, and send it a variable called listTitle, whose value is "day"
res.render("list", { //uses view engine to render a particular page (views/list.ejs)
listTitle: "my daily routine",
newListItems: items
});
}); //app.get()
app.post("/", function(req, res){
const item = req.body.nextItem; //the item user inputs in the form
if (req.body.list === "Work list") {
workItems.push(item);
res.redirect("/work");
} else { //req.body.list === the current day
items.push(item);
res.redirect("/");
//when post request is triggered on /, we log the value of item
//and redirect to / which gets us to app.get()
//which will render list.ejs with both variables
}
console.log(req.body.list);
}); //app.post()

//2. work list
app.get("/work", function(req, res){
res.render("list", {
listTitle: "Work list",
newListItems: workItems
});
}); //app.get()
app.post("/work", function(req, res){
const item = req.body.nextItem;
workItems.push(item);
res.redirect("/work");
}); //app.post
//3. about page
app.get("/about", function(req, res){
res.render("about");
}); //app.get()
//3. spinning up server
app.listen(3000, function(){
console.log("EJS");
}); //app.listen()

下面是当我执行'node app.js'时在我的终端上显示的消息:

$ node app.js
EJS
MongooseError: Operation `items.insertMany()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:UsersUsuarioDesktoptoDoList-v2node_modulesmongooselibdriversnode-mongodb-nativecollection.js:198:23)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7) {
insertedDocs: [
{ _id: 6135276e4595e61b748e4381, name: 'wake up (8 am)', __v: 0 },
{
_id: 6135276e4595e61b748e4382,
name: 'cry (8:30 am - 12 pm)',
__v: 0
},
{
_id: 6135276e4595e61b748e4383,
name: 'listen to “sexyback” on loop (12 pm - 7 pm)',
__v: 0
},
{
_id: 6135276e4595e61b748e4384,
name: 'punch a hole in the wall and patch it up (7 pm - 7:30 pm)',
__v: 0
},
{ _id: 6135276e4595e61b748e4385, name: 'sleep (8 pm)', __v: 0 }
]
}
(node:7028) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27107
at NativeConnection.Connection.openUri (C:UsersUsuarioDesktoptoDoList-v2node_modulesmongooselibconnection.js:846:32)
at C:UsersUsuarioDesktoptoDoList-v2node_modulesmongooselibindex.js:351:10
at C:UsersUsuarioDesktoptoDoList-v2node_modulesmongooselibhelperspromiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:UsersUsuarioDesktoptoDoList-v2node_modulesmongooselibhelperspromiseOrCallback.js:31:10)

我在不同的选项卡中运行mongod和mongo。两者都很好,但当我使用"show "命令,我没有得到我创建的这个新的todolistDB数据库。

我格式化了我的电脑,丢失了所有的程序和安装。这种代码过去是有效的。现在我必须添加一个PATH到MongoDB,以便它开始工作,但我不记得如何为猫鼬做同样的事情。

我把27017写成了27107

最新更新