It's possible that this question is a duplicate, but I haven't found a suitable answer to my issue yet.
Currently, I have an ExpressJS server set up to handle API requests for fetching data from a MongoDB database. My database connection is made using mongoosejs for querying and saving data.
I'm working on a route that should retrieve all data matching some user input, but I'm facing difficulties with the query itself. Despite extensive research online, I haven't been able to find a similar problem.
Below, I'll provide an example of the code I currently have:
Route Code
// -- Return matched data (GET)
router.get('/match', async (req, res) => {
const style_data = req.query.style; // Get URL param for style scores ** Comes in as a string **
const character_data = req.query.character; // Get URL param for character scores ** Comes in as a string **
// Run match systems
const style_matches = style_match(style_data);
res.send({
response: 200,
data: style_matches
}); // Return data
});
Query Code
// ---(Build the finder)
const fetch_matches_using = async function(body, richness, smoke, sweetness) {
return await WhiskyModel.find({
'attributes.body': body,
'attributes.richness': richness,
'attributes.smoke': smoke,
'attributes.sweetness': sweetness
});
}
// ---(Start match function)---
const style_match = async function (scores_as_string) {
// ---(Extract Data)---
const body = scores_as_string[0];
const richness = scores_as_string[1];
const smoke = scores_as_string[2];
const sweetness = scores_as_string[3];
const matched = [];
// ---(Initialize Variables)---
let match_count = matched.length;
let first_run; // -> Exact matches
let second_run; // -> +- 1
let third_run; // -> +- 2
let fourth_run; // -> +- 3
// ---(Begin DB Find Loop)---
first_run = fetch_matches_using(body, richness, smoke, sweetness).then((result) => {return result});
matched.push(first_run);
// ---(Return Final Data)---
return matched
}
Example of DB Object
{
_id: mongoid,
meta-data: {
pagemd:{some data},
name: whiskyname
age: whiskyage,
price: price
},
attributes: {
body: "3",
richness: "3",
smoke: "0",
sweetness: "3",
some other data ...
}
}
When I make a request to the route in Postman, the JSON data appears as follows:
{
response: 200,
data: {}
}
Additionally, when I use console.log() to check the output of matched within the style match function after pushing it, I get [ Promise(pending) ], which confuses me.
If I log the result within .then(), I receive an empty array.
I attempted using the populate() method post finding, which does work technically, but instead of returning only matching data, it brings back every entry in the collection. I believe I may be making a mistake here, but I also don't see why I would need to utilize the .populate() function to access the nested object.
Is there something fundamentally incorrect in my implementation?
Furthermore, it's worth mentioning that the route and matching functions are stored in separate files for simplicity's sake.
Thank you in advance for any insights provided.