我试图启动一个简单的数据库,但是当我启动自己的nodejs文件时,我一直从mongoose.connect()收到此错误消息connect ECONNREFUSED 127.0.0.1:3000
。我是猫鼬的新手,但是我很确定我的代码是正确的。这是我的代码:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:3000/cats_app', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Connected to DB!')
})
.catch(err => {
console.log("Couldn't console to DB:", err.message)
})
const catSchema = new mongoose.Schema({
name: String,
age: Number,
mood: String
})
var Cat = mongoose.model("Cat", catSchema)
var george = new Cat({
name: "George",
age: 11,
mood: "Grouchy"
})
george.save()
.then(item => {
console.log("saved:", item);
})
.catch(err => {
console.log("error:", err.message);
});
答案 0 :(得分:1)
默认情况下,MongoDB在端口27017上为accepts connections。您正在尝试在端口3000上进行连接。
尝试此操作,更改MongoDB URL中的:3000
端口规范。
mongoose.connect('mongodb://localhost:27017/cats_app', {
useNewUrlParser: true,
useUnifiedTopology: true
})