I've been working on testing an API that is supposed to return all user documents from my Mongo DB. However, I keep running into the issue of receiving an empty result every time I test it. I've been struggling to pinpoint where exactly in my code the problem lies.
Here's a snippet of my user model:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
let userSchema = new Schema({
userId: {
type: String
},
pw: {
type: String
},
}, {
collection: "creds"
});
module.exports = mongoose.model("User", userSchema);
And below is the section of my API responsible for retrieving all users:
router.get("/session/users", async (req, res) => {
try {
User.find({}, function (err, user) {
if (err) {
console.log(err);
res.status(500).send({
message: "Interal server error:" + err.message,
});
} else {
console.log(user);
res.json(user);
}
});
} catch (e) {
console.log(e);
console.log(req.body.userId);
res.status(500).send("Internal server error: " + e.message);
}
});
Whenever I run this within the SOAP environment, I consistently see the following output in the console. Despite having one existing document, the result seems to suggest otherwise:
Connection to the database instance was successful
[]
GET /api/session/users 200 90.584 ms - 2