Getting empty array in browser when testing database connection

Hi, I’m trying out my first MERN app and I’m running into trouble. I’ve been following the MongoDB documentation to set up my project and for the routes. The server is running and I’m connected to MongoDB but when I go to browser (http://localhost:4000/plant) to test that I’m able to get the data I just get an empty array. So I’m thinking I’m using an incorrect database or collection name or something? I would love an extra set of eyes if possible.

Attached is a pic of my database page and I used the given connection URI to my cluster:

ATLAS_URI=mongodb+srv://allyson:*********@cluster0.87dg5hc.mongodb.net/?retryWrites=true&w=majority
PORT=4000

Here is a section of my routes file:

const express = require("express");

// recordRoutes is an instance of the express router.
// We use it to define our routes.
// The router will be added as a middleware and will take control of requests starting with path /plants.
const plantRoutes = express.Router();

// This will help us connect to the database
const dbo = require("../db/conn");

// This help convert the id from string to ObjectId for the _id.
const ObjectId = require("mongodb").ObjectId;

// This section will help you get a list of all the records.
plantRoutes.route("/plant").get(function (req, res) {
  let db_connect = dbo.getDb("plant-babies-data");
  db_connect
    .collection("plants")
    .find({})
    .toArray(function (err, result) {
      if (err) throw err;
      res.json(result);
    });
});

Thanks so much for any help!!

I think I found my error- I had used the collection name instead of the database name when connecting to MongoDB. I can now see all my data objects in the browser.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.