How can I modify this function to retrieve all users? I am currently in the process of learning async await and struggling with understanding how to access the request body. Here's my function:
export const get: Operation = async (
req: express.Request,
res: express.Response
) => {
commonUtility.showRequestParam(req);
let users: db.IUserDocument[] = [];
try {
// Add explanation for fetching and storing data from MongoDB here.
users = await UserModel.find()
.then(data => {
return data;
})
.catch(err => {
throw err;
});
} catch (err) {
// Handle errors.
api.responseError(res, err);
}
if (users.length < 1) {
// What should happen in this case?
api.responseJSON(res, 200, []);
}
};
Here is my user model:
export const usersSchema = new Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
BaseFields
});
export const UserModel = mongoose.model<db.IUserDocument>('Users', usersSchema);