camstar915
camstar915

Reputation: 153

MongoDB not returning data MEAN Stack App

VERY new to the MEAN stack, and to programming. I'm working on connecting a mongoDB database to a Node/Express backend, and I can't get mongoDB to return anything other than an empty array.

This is my Site model

const mongoose = require('mongoose');
const siteSchema = mongoose.Schema({
  siteId: { type: Number, required: true },
  utility: { type: String, required: true},
  company: { type: String, required: true},
  address: {
    street: String,
    city: String,
    state: String,
    zip: String
  },
  contact: {
    person: String,
    phone: String,
    cell: String,
    fax: String,
    email: String
  },
  siteType: String,
  siteUse: String,
  utilityStatus: String,
  hazards: [{
    hazardId: Number,
    altAddress: {
      street: String,
      city: String,
      state: String,
      zip: String
    },
    locationOnSite: String,
    notes: String,
    hazard: String,
    check: String,
    trapType: String,
    frequency: String,
    trapSize: String,
    testDue: String,
    testExtendedTo: String,
    installDue: String,
    installExtendedTo: String,
    deviceDeficiency: String,
    deviceDeficiencyExtendedTo: String,
    lastTest: String,
    lastTestBy: String,
    lastTestPass: String,
    pumpedBy: String,
    deviceStatus: String,
    lastletterType: String,
    lastLetterSent: String,
    updatedBy: String,
    updatedDate: String,
    webfee: Number,
    mailRecipients: [{
      contact: String,
      Company: String,
      address: {
        street: String,
        city: String,
        state: String,
        zip: String
      },
      phone: String,
      fax: String,
      email: String
    }]
  }]
});

module.exports = mongoose.model('Site', siteSchema);

This is my sites.js route

const express = require('express');

const Site = require('../models/site');

const router = express.Router();

router.get("", (req, res, next) => {
  const siteQuery = Site.find();
  console.log(siteQuery);
  let fetchedSites;
  siteQuery.then(documents => {
    fetchedSites = documents;
    return Site.count();
  })
  .then(count => {
    res.status(200).json({
      message: 'Filler',
      sites: fetchedSites,
      siteCount: count
    });
  });
});

module.exports = router;

I'm connecting to the database successfully, but this is the only output I get

{"message":"Filler","sites":[],"siteCount":0}

When I console.log(siteQuery) the mongoose Query object is printed.

Any help would be GREATLY appreciated

Upvotes: 0

Views: 80

Answers (2)

camstar915
camstar915

Reputation: 153

There was an issue with the connection string I got from mongoDB. I had to add all the shards in my database into the connection string after '@' and separate them by commas, and had to replace 'test' in the default connection string to the name of my database and it worked!

If you're having a similar issue with mongoDB Atlas, I ended up having to use the chat support from mongo and they were able to find the problem after ~30 minutes.

Upvotes: 0

SuleymanSah
SuleymanSah

Reputation: 17888

Can you try like this? I converted your code to async/await syntax. Also Model.count is deprecated, so I used countDocuments.

router.get("/", async (req, res, next) => {
  try {
    const fetchedSites = await Site.find({});

    const count = await Site.countDocuments();

    res.status(200).json({
      message: "Filler",
      sites: fetchedSites,
      siteCount: count
    });
  } catch (err) {
    console.log(err);
    res.status(500).send("Something went wrong");
  }
});

Upvotes: 0

Related Questions