MongoDB Atlas未显示集合

时间:2019-06-24 04:34:14

标签: node.js mongodb express post mongoose

我已经创建了一个POST请求以在我的REST API中创建用户,但是一旦发出请求并在Postman中设置了参数,我就进入MongoDB Atlas,它说有0个数据库和0个集合。

不知道我在做什么错,但是我已将数据库设置为通过.env文件进行连接。

//router.js

router.post("/register", (req, res) => {
  const newUser = new User({
    name: req.body.name,
    email: req.body.email,
    password: req.body.password
  });
  newUser
    .save()
    .then(user => res.json(user))
    .catch(err => res.status(404).json({ success: false }));
});

module.exports = router;

//server.js

    require("dotenv").config();

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const userController = require("./routes/users");

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

mongoose.connect(process.env.MONGODB_URI, {
  useNewUrlParser: true
});

const db = mongoose.connection;
db.on("error", error => {
  console.log(error);
});
db.once("open", () => {
  console.log("Connected to Mongoose");
});

app.use(express.json());

app.use(express.static(__dirname + "/client/build/"));

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/client/build/index.html");
});

app.use("/api/users", userController);

const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
  console.log(`Server started on port ${PORT}`);
});

当我访问localhost时,我看到正在创建用户,列出了我使用Postman创建的所有用户。但是我的MongoDB仍然说没有数据库,也没有集合。

0 个答案:

没有答案