const Article = require('../model/Article');
const User = require('../model/User');
const jwt = require ('jsonwebtoken');
const ROLES_LIST = require('../config/roles_list');
const verifyRoles = require('../middleware/verifyRoles');
// Other methods in this space //
const getArticle = async (req, res) => {
if (!req?.params?.id) return res.status(400).json({ message: 'Article ID required.'});
const article = await Article.findOne({ _id: req.params.id}).exec();
if (!article) {
return res.status(204).json({ message: `Article ID ${req.params.id} not found.`});
}
res.json(article);
//For some reason, an ID that doesn't exist in the database tries to reply with the structure of the entire database.
};
// One more method here //
module.exports = {
handleNewArticle,
getAllArticles,
deleteArticle,
getArticle,
editArticle
};
This is the relevant piece of code. It returns an article with the id provided in the url parameters. It works perfectly when an _id existing in the database “articles” is provided, returning a json in thunder client. Otherwise, it returns a massive block of text in the terminal, with information I have little understanding of – though I see within it information about how I’m connected to the MongoDB, what address it is, who is the host, what are my connectionOptions, what is the ConnectionString in plaintext (which in itself contains the Admin account’s name and password), and much more.
I make this request via Thunder Client on VSCode, making a GET request with /articles/(id) after the relevant address – expecting a json response.
Sanitized DATABASE_URI for connecting to Mongodb
`DATABASE_URI=mongodb+srv://:@..mongodb.net/<database_name>?retryWrites=true&w=majority
I’ve tried to see if expanding my DATABASE_URI link from just the database inside the Cluster to the whole Cluster would work, but it only creates a new, empty “test” database with the articles and users models inside.
I’ve tried searching the mongoose docs for findOne syntax errors, but I didn’t find anything obvious, And I don’t know why an existing ID would gives a correct response, while an Id that doesn’t exist doesn’t return the expected “null”.
Searching for a solution on google (results on stack overflow) gives me either people who “always Only Get Null” which is not my problem, Or a post i’ve seen that looked promising, but they were using try
into await model.then
, and got told to not use Then
. As you can see, it doesn’t seem applicable to me.
I’ve also come across a post from 11 years ago, having a similar odd response. But. They solve it by putting a Console.log in the functions callback, and if you check mongoosejs docs – // Model.findOne() no longer accepts a callback
.
The Mongoose Docs on “model.findOne” informs us to use “findById” if we’re searching for _id, but when I replaced the line with const article = await Article.findById(req.params.id).exec();
, the same odd behavior happens. Of course, a correct ID with this method ALSO returns a correct response, so, the issue does not lay with me using “findOne” instead of “findById” as recommended by mongoose documentation.
I’ve tried changing the if(!article)
to if (article === null)
, but that didn’t change anything.
similarly if i wrote if(article !== null)
, if an id that doesn’t exist in the database is searched for, it writes the same stuff into the terminal in VSCode.