无法使用猫鼬连接到mongoDB客户端

时间:2019-12-24 06:55:34

标签: node.js mongodb mongoose

我是mongoosemongo 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();

1 个答案:

答案 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已连接