How can I confirm I am pulling data from the right collection in MongoDB?

I am doing a test with Insomnia and it is giving me an empty array as shown below here

And I am also connected to MongoDB as shown here
snip1

I am only looking to do a read route… and I see now errors in my VScode or the terminal.

So… how/where do I begin to troubleshoot this?

The collection from MongoDB is called “part” under a “test” database… How do I know I am pulling any information from that collection?

The Schema I have is this:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const partSchema = new Schema({
  Reference: { type: String },
  Description: { type: String },
  Replacements: { type: String },
});

const Part = mongoose.model("Part", partSchema);

module.exports = Part;

The route I have is this:

const router = require("express").Router();
let Part = require("../Models/partschema");

router.route("/").get((req, res) => {
  Part.find()
    .then((part) => res.json(part))
    .catch((err) => res.status(400).json("error: + err"));
});
module.exports = router;

And I got this on my server.js

const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");

require("dotenv").config();

const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true });

const connection = mongoose.connection;
connection.once("open", () => {
  console.log("MongoDB database connection established successfully");
});


////  This is the important part below for the GET request

const partRouter = require("./Routes/read");
app.use("/part", partRouter);

app.listen(port, () => {
  console.log(`Server is running on port: ${port}`);
});

When I see no errors on the terminal, VScode, or console… how do I troubleshoot this thing?

The problem might be coming from the collection name

I’ll try to put an “s” at the end of the collection name…

Mongoose automatically looks for an “s” at the end of the collection… so… apparently mongoose might be creating an empty collection with an “s” at the end, and showing me an empty array… this is annoying… .

You should be able to interact with your mongoDB either with mongo shell (for local installations) or through the interface at mongodb.com if you’re using an atlas db. So, you can put some docs in the database and then query for them and you should see what you put in the database in your app if you output them. You can also query for collections via mongoose to make sure you’re using the one you expect. Conversely, if you save some data via your app, it should show up in atlas or mongo shell too.

The only code problem that leaps out is

According to the docs, find() apparently requires a filter, even if empty (or maybe undefined is a good enough object for it…). You could always add some logging to your catch callback if you think you are missing the response:

    .catch((err) => {
      console.error(err);
      return res.status(400).json("error: + err");
});

You could also add some logging to your /part route and have it log the route inputs and just log ‘hello’ or something from your then() callback to make sure it’s executing. Once you’re sure the code path is going where you think it’s going it may be easier to diagnose the mongoDB problems.

1 Like

Thanks jeremy…

for this one I had to do some modifications to the Schema file… seems like I would run into different problems if my MongoDB collection doesn’t have an “s” at the end…

If I want to work naming my collection without the “s” at the end… then I have to implement a code like this… notice I am using mongoose.pluralize(null); plus I have to use the const part with a small “p”, else mongoose would once again create another collection with a capital “P”…

const mongoose = require("mongoose");
mongoose.pluralize(null);
const Schema = mongoose.Schema;

const partSchema = new Schema({
  Reference: { type: String },
  Description: { type: String },
  Replacements: { type: String },
});

const part = mongoose.model("part", partSchema);

module.exports = part;

Next… working on filtering and pagination from the backend for this one… .