I am creating a MERN magazine web application that shows users articles that are stored in a mongodb. I have multiple databases, called userDB and articleDB on Mongo Atlas. These databases are located in a single cluster. The problem is, in one of my controllers, articleController.js, the find method on the collection is always returning an empty array. This results in my app breaking as it always worked and did not return an empty array, but rather, an array of three of the latest article objects before it I started transitioning my data to Atlas from my local mongodb.
This is a code snippet of my articleControllers.js:
const express = require('express');
const app = express();
const cors = require("cors");
let articleSchema = require('../models/article.model');
const mongoose = require("mongoose");
const conn = mongoose.createConnection("mongodb+srv://<username>:<password>@cluster0.c94ps.mongodb.net/articleDB?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true, dbName: "articleDB" });
const Article = conn.model('article', articleSchema); // update: this is where the problem was.
const fs = require("fs");
const path = require("path");
app.use(cors({
origin: 'http://localhost:3000',
credentials: true
}));
app.use('/images', express.static(path.join(__dirname, '/images')));
exports.article_home_section_display_get = (req, res) => {
const id = req.params.id;
const firstLetter = id.charAt(0).toUpperCase();
const restOfWord = id.substring(1, id.length);
const fullWord = firstLetter + restOfWord;
Article.find({article_section: fullWord}).sort({article_date: -1}).limit(3)
.then(articles => {
console.log(articles); //empty array
console.log(`Found and sent over three of the latest ${fullWord} articles.`);
res.send(articles);
})
.catch(err => {
console.log(err);
});
};
where username and password in angle brackets are my actual username and password.
Could it be that it's actually impossible to connect to mongo Atlas using mongoose.createConnection when connecting to multiple databases? Or maybe, my application is not connected to the Atlas at all because of some kind of an error that I overlooked? By the way, database on Atlas is called, articleDB, and the collection inside is article.
I have been struggling on this for a long time and cannot figure out what went wrong when I did what other people did. So, any help would be greatly appreciated.