0
votes

I a trying to post a new entry to MongoDB, I know that this issue is where I am calling my routes in the server file, but I cannot figure out how to fix the issue. I need to be able to test the API in postman and post to the database.

I am getting the following errors:

/Users/ryan/Desktop/leasequery/operations-backend/node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) ^

TypeError: Router.use() requires a middleware function but got a Object at Function.use (/Users/ryan/Desktop/leasequery/operations-backend/node_modules/express/lib/router/index.js:458:13) at Function. (/Users/ryan/Desktop/leasequery/operations-backend/node_modules/express/lib/application.js:220:21) at Array.forEach () at Function.use (/Users/ryan/Desktop/leasequery/operations-backend/node_modules/express/lib/application.js:217:7) at Object. (/Users/ryan/Desktop/leasequery/operations-backend/server.js:12:5) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3) [nodemon] app crashed - waiting for file changes before starting...

I am using a middleware (express-validator)

server.js

const express = require("express");
const connectDB = require("./config/db");

const app = express();

connectDB();

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

app.get("/", (req, res) => res.json({ msg: "Welcome to Operations" }));

app.use("/api/status", require("./routes/status"));

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log(`Server listening on Port ${PORT}`));

routes/status.js

const express = require("express");
const router = express.Router();
const { check, validationResult } = require("express-validator");

const Status = require("../models/Status");

router.post(
  "/",
  [
    check("name", "Please Add Name")
      .not()
      .isEmpty(),
    check("date", "Please add date")
      .not()
      .isEmpty(),
    check("comment", "Please add comment")
      .not()
      .isEmpty()
  ],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    const { name, date, comment } = req.body;

    try {
      entry = new Entry({
        name,
        date,
        comment
      });

      await entry.save();
    } catch (err) {
      console.error(err.message);
      res.status(500).send("Server Error...");
    }
  }
);

models/Status.js

const mongoose = require("mongoose");

const StatusSchema = mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    required: true
  },
  comment: {
    type: String,
    required: true
  }
});

module.exports = mongoose.model("status", StatusSchema);

1

1 Answers

0
votes

routes/status.js

const router = express.Router();
const { check, validationResult } = require("express-validator");

const Status = require("../models/Status");

router.post(
  "/",
  [
    check("name", "Please Add Name")
      .not()
      .isEmpty(),
    check("date", "Please add date")
      .not()
      .isEmpty(),
    check("comment", "Please add comment")
      .not()
      .isEmpty()
  ],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    const { name, date, comment } = req.body;

    try {
      entry = new Entry({
        name,
        date,
        comment
      });

      await entry.save();
    } catch (err) {
      console.error(err.message);
      res.status(500).send("Server Error...");
    }
  }
);

module.exports = router;