I have a problem like this. I am making an API with express. In there, I have created a MongoDB connection using mongoose. In this API I have created a function to sort elements by date. This is my function is looks,
module.exports.findAll = function (callback) {
const query = {state: "Confirmed"};
Reservation.find(query, {sort: '-date'} , callback);
}
When I check this with POSTMan, it just returns the ids of particular elements.
This is my controller file.
const express = require('express');
var router = express.Router();
var Reservation = require('../models/reservation');
router.get("/", function (req, res) {
Reservation.findAll(function (err, reservations) {
if(err) throw err;
if (!reservations){
res.json({state:false,msg:"No reservations found"});
}
if(reservations){
res.json(reservations);
}
})
});
router.post("/create",function (req,res) {
const newReservation = new Reservation({
date: req.body.date,
from: req.body.from,
to: req.body.to,
lab: req.body.lab,
name: req.body.name,
email: req.body.email,
role: req.body.role,
year: req.body.year,
reason: req.body.reason,
state: "Not Comfirm"
});
Reservation.saveReservation(newReservation, function (err, reservation) {
if (reservation) {
res.send(reservation);
}
else {
console.log('Error in User save :' + JSON.stringify(err, undefined, 2));
}
});
});
router.get('/notComfirmed', function (req, res) {
Reservation.findNotComfirmed(function (err, reservations) {
if(err) throw err;
if (!reservations){
res.json({state:false,msg:"No reservations found"});
}
if(reservations){
res.json(reservations);
}
})
});
router.post('/confirm', function(req, res){
const _id= req.body._id;
Reservation.updateReservation(_id, function (err, reservation) {
if(err) throw err;
if (!reservation){
res.json({state:false,msg:"Something went wrong"});
}
if(reservation){
res.json(reservation);
}
})
});
router.post('/remove', function(req, res){
const _id= req.body._id;
Reservation.remove(_id, function (err, reservation) {
if(err) throw err;
if (!reservation){
res.json({state:false,msg:"Something went wrong"});
}
if(reservation){
res.json(reservation);
}
})
});
module.exports = router;
This is how my model looks,
const mongoose = require('mongoose');
var DateOnly = require('dateonly');
const schema = mongoose.Schema;
const reservationSchema = new schema({
date:{type:String, required:true},
from:{type:String, reason:true},
to:{type:String, required:true},
lab:{type:String, required:true},
name:{type:String, required:true},
email:{type:String, required:true},
role:{type:String, required:true},
year:{type:String},
reason:{type:String, required:true},
state:{type:String, required:true},
});
const Reservation = module.exports= mongoose.model("Reservation",reservationSchema );
module.exports.saveReservation = function (newReservation, callback) {
newReservation.date=new DateOnly(newReservation.date);
newReservation.save(callback);
}
module.exports.findBYDateAndTime = function(date, time, lab, callback){
const query = {date:date,time:time, lab:lab};
Reservation.findOne(query,callback);
}
module.exports.findNotComfirmed= function (callback) {
const query = {state: "Not Comfirm"};
Reservation.find(query, callback);
}
module.exports.updateReservation = function (id, callback) {
var reservation ={
state: "Confirmed"
}
Reservation.findByIdAndUpdate(id, { $set: reservation}, {new: true}, callback) ;
}
module.exports.remove = function (_id, callback) {
Reservation.findByIdAndRemove(_id, callback);
}
module.exports.findAll = function (callback) {
const query = {state: "Confirmed"};
Reservation.find(query, {sort: '-date'} , callback);
}
Can someone, help me to get all the details of confirmed reservation with sort them by the date? I done it so many ways but it always returns only the ids of those reservations.
Thank You!
In mongoose query, second parameter is reserved for fields which we want to select from mongodb.
I have found some answer on stackoverflow How to sort in mongoose?
Try this one
Reservation.find({}, null, {sort: {date: -1}}, function(err, docs) { ... });