Having trouble with the part of the test that says " You can make a GET request to api/exercise/users to get an array of all users. Each element in the array is an object containing a user’s username and _id
Here’s my code for the section:
app.get("/api/exercise/users", (req, res) => {
Per.find({}, (err, data) => {
if(!err){
res.json(data)
}
})
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.
What error are you getting in the Chrome developer console window when you are submitting your project for testing?
Using res.json inside a callback function often leads to unexpected behaviour in my experience.
How about you try saving the result of your mongoose query to a constant variable, then sending that variable in res.json or simply using something like res.send(savedQueryResultVariable)?
To get the best advice, please post a link to your live project (like repl.it or glitch) so we can experiment.
The problem right now is that you may be mixing multiple ways of handling the asynchronous find() that you are trying to perform. Getting a Promise { <pending> } means you’ve tried to use the promises or async/await interfaces and not called a .then() or used await, and it’s really muddled because you began with a callback. To begin solving the problem, you need to decide which interface to use and then use it according to the Mongoose documentation.
If you want to use await/async (since you have the find(...).exec(), you’ll need to use await on that function call like
const people = await People.find({}).exec();
That should resolve the promise and store the found records in people, but then you will also have to make this route an async function. Then hopefully you would get something other than the unresolved promise logged to console.
You can use your call back you originally posted as well;
Just place a console.log(data); before your response and see what happens.
Well, you’ve got your data then. You should be able to build an array of JSON objects with the username and _id fields which you can then return in your response.