我是mongoose
和mongo DB
的初学者。当我尝试使用mongoDB
连接到mongoose
时,在节点app.js file
的帮助下运行app.js
时,进程开始了。
我没有任何输出。当我检查database
时,也不会创建database
。
谁能告诉我我要去哪里错了?
这是我的代码:
const mongoose = require('mongoose');
var conn = mongoose.connect('mongodb://localhost:27017/my_database', {
useNewUrlParser: true,
useUnifiedTopology: true
});
const peopleSchema = new mongoose.Schema ({
name: String,
age: Number
});
const People = mongoose.model("People",peopleSchema);
const people = new People({
name : "Umang",
age: 25
})
people.save();
答案 0 :(得分:-1)
在命令提示符下尝试以下命令
npm install express
然后添加以下代码段。
const mongoose = require('mongoose');
const express = require('express')
const app = express()
var conn = mongoose.connect('mongodb://localhost:27017/my_database', {
useNewUrlParser: true,
useUnifiedTopology: true
});
const peopleSchema = new mongoose.Schema({
name: String,
age: Number
});
const People = mongoose.model("People", peopleSchema);
const people = new People({
name: "Umang",
age: 25
})
people.save();
app.listen(3000, function () {
console.log('Node server listening on port 3000');
})
我已经对其进行了测试,并且可以正常工作。
确保MongoDb
已连接