mongoose find next object [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers



  • mongoose request order by

    1 answer




I am creating a photography website and eventually I would like to add the functionality for the user to see the next photo in the database. I am new to web development and therefore want to proceed step by step, so for now it would be enough if the id of the next photo would be displayed in the console. This is the code I use right now:



var getPhoto = require("../public/modules/getPhoto")

router.get("/:id/next", function(req, res){
getPhoto.next();
res.redirect("/");
})


and



var Photo = require("../../models/photo");
var exports = module.exports = {};

exports.next = function(callback){
console.log(Photo.find({}).sort({_id: 1 }).limit(1)._id);
}


However, this only returns undefined.



I read here that I need to use callbacks, but I don't know how to implement it, even with the code given in the example in the link.



How can I print the id of the next photo to console?



Edit
My code now looks like this:



// NEXT PHOTO - shows next photo 
router.get("/:id/next", function(req, res){
Photo.find({}).sort({ _id: 1 }).limit(1).then(function(docs){
console.log(docs[0]._id)
res.redirect("/photos/" + docs[0]._id)
})
})

// PREVIOUS PHOTO - shows previous photo
router.get("/:id/previous", function(req, res){
Photo.find({}).sort({ _id: -1 }).limit(1).then(function(docs){
console.log(docs[0]._id)
res.redirect("/photos/" + docs[0]._id)
})
})


However, this only gives me the first or last item in the database. According to this link I have to substitute { _id: -1 } for {_id: {$gt: curId}}. I am not using jQuery, so how can I rewrite this?










share|improve this question















marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 3:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    You don't need to use callbacks, you can also make use of the Promise being returned from Mongoose.... but if you aren't familiar with either of those concepts then personally I'd advise you start with learning the basics of Node. Also, there are examples in the mongoose docs.

    – James
    Nov 22 '18 at 20:02













  • Don't believe what you read in the first post you find. From 2016 if the answer had any credibilty then it would have significantly more votes than 1. As the comment ays "read the documentation first". You can ask questions and read others answers once you see what the documentation has to say. And it says Photo.find({}).sort({ _id: 1 }).limit(1).then( docs => console.log(docs[0]._id) ). As does the other main point of the more cannonical and highly voted answer as now linked.

    – Neil Lunn
    Nov 23 '18 at 3:05













  • Or Photo.findOne({}).sort({ _id: 1 }).then( doc => console.log(doc) ) for that matter

    – Neil Lunn
    Nov 23 '18 at 3:09


















1
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers



  • mongoose request order by

    1 answer




I am creating a photography website and eventually I would like to add the functionality for the user to see the next photo in the database. I am new to web development and therefore want to proceed step by step, so for now it would be enough if the id of the next photo would be displayed in the console. This is the code I use right now:



var getPhoto = require("../public/modules/getPhoto")

router.get("/:id/next", function(req, res){
getPhoto.next();
res.redirect("/");
})


and



var Photo = require("../../models/photo");
var exports = module.exports = {};

exports.next = function(callback){
console.log(Photo.find({}).sort({_id: 1 }).limit(1)._id);
}


However, this only returns undefined.



I read here that I need to use callbacks, but I don't know how to implement it, even with the code given in the example in the link.



How can I print the id of the next photo to console?



Edit
My code now looks like this:



// NEXT PHOTO - shows next photo 
router.get("/:id/next", function(req, res){
Photo.find({}).sort({ _id: 1 }).limit(1).then(function(docs){
console.log(docs[0]._id)
res.redirect("/photos/" + docs[0]._id)
})
})

// PREVIOUS PHOTO - shows previous photo
router.get("/:id/previous", function(req, res){
Photo.find({}).sort({ _id: -1 }).limit(1).then(function(docs){
console.log(docs[0]._id)
res.redirect("/photos/" + docs[0]._id)
})
})


However, this only gives me the first or last item in the database. According to this link I have to substitute { _id: -1 } for {_id: {$gt: curId}}. I am not using jQuery, so how can I rewrite this?










share|improve this question















marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 3:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    You don't need to use callbacks, you can also make use of the Promise being returned from Mongoose.... but if you aren't familiar with either of those concepts then personally I'd advise you start with learning the basics of Node. Also, there are examples in the mongoose docs.

    – James
    Nov 22 '18 at 20:02













  • Don't believe what you read in the first post you find. From 2016 if the answer had any credibilty then it would have significantly more votes than 1. As the comment ays "read the documentation first". You can ask questions and read others answers once you see what the documentation has to say. And it says Photo.find({}).sort({ _id: 1 }).limit(1).then( docs => console.log(docs[0]._id) ). As does the other main point of the more cannonical and highly voted answer as now linked.

    – Neil Lunn
    Nov 23 '18 at 3:05













  • Or Photo.findOne({}).sort({ _id: 1 }).then( doc => console.log(doc) ) for that matter

    – Neil Lunn
    Nov 23 '18 at 3:09














1












1








1









This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers



  • mongoose request order by

    1 answer




I am creating a photography website and eventually I would like to add the functionality for the user to see the next photo in the database. I am new to web development and therefore want to proceed step by step, so for now it would be enough if the id of the next photo would be displayed in the console. This is the code I use right now:



var getPhoto = require("../public/modules/getPhoto")

router.get("/:id/next", function(req, res){
getPhoto.next();
res.redirect("/");
})


and



var Photo = require("../../models/photo");
var exports = module.exports = {};

exports.next = function(callback){
console.log(Photo.find({}).sort({_id: 1 }).limit(1)._id);
}


However, this only returns undefined.



I read here that I need to use callbacks, but I don't know how to implement it, even with the code given in the example in the link.



How can I print the id of the next photo to console?



Edit
My code now looks like this:



// NEXT PHOTO - shows next photo 
router.get("/:id/next", function(req, res){
Photo.find({}).sort({ _id: 1 }).limit(1).then(function(docs){
console.log(docs[0]._id)
res.redirect("/photos/" + docs[0]._id)
})
})

// PREVIOUS PHOTO - shows previous photo
router.get("/:id/previous", function(req, res){
Photo.find({}).sort({ _id: -1 }).limit(1).then(function(docs){
console.log(docs[0]._id)
res.redirect("/photos/" + docs[0]._id)
})
})


However, this only gives me the first or last item in the database. According to this link I have to substitute { _id: -1 } for {_id: {$gt: curId}}. I am not using jQuery, so how can I rewrite this?










share|improve this question

















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers



  • mongoose request order by

    1 answer




I am creating a photography website and eventually I would like to add the functionality for the user to see the next photo in the database. I am new to web development and therefore want to proceed step by step, so for now it would be enough if the id of the next photo would be displayed in the console. This is the code I use right now:



var getPhoto = require("../public/modules/getPhoto")

router.get("/:id/next", function(req, res){
getPhoto.next();
res.redirect("/");
})


and



var Photo = require("../../models/photo");
var exports = module.exports = {};

exports.next = function(callback){
console.log(Photo.find({}).sort({_id: 1 }).limit(1)._id);
}


However, this only returns undefined.



I read here that I need to use callbacks, but I don't know how to implement it, even with the code given in the example in the link.



How can I print the id of the next photo to console?



Edit
My code now looks like this:



// NEXT PHOTO - shows next photo 
router.get("/:id/next", function(req, res){
Photo.find({}).sort({ _id: 1 }).limit(1).then(function(docs){
console.log(docs[0]._id)
res.redirect("/photos/" + docs[0]._id)
})
})

// PREVIOUS PHOTO - shows previous photo
router.get("/:id/previous", function(req, res){
Photo.find({}).sort({ _id: -1 }).limit(1).then(function(docs){
console.log(docs[0]._id)
res.redirect("/photos/" + docs[0]._id)
})
})


However, this only gives me the first or last item in the database. According to this link I have to substitute { _id: -1 } for {_id: {$gt: curId}}. I am not using jQuery, so how can I rewrite this?





This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers



  • mongoose request order by

    1 answer








javascript node.js mongoose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 2 '18 at 12:30







newman

















asked Nov 22 '18 at 19:57









newmannewman

357




357




marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 3:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 3:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    You don't need to use callbacks, you can also make use of the Promise being returned from Mongoose.... but if you aren't familiar with either of those concepts then personally I'd advise you start with learning the basics of Node. Also, there are examples in the mongoose docs.

    – James
    Nov 22 '18 at 20:02













  • Don't believe what you read in the first post you find. From 2016 if the answer had any credibilty then it would have significantly more votes than 1. As the comment ays "read the documentation first". You can ask questions and read others answers once you see what the documentation has to say. And it says Photo.find({}).sort({ _id: 1 }).limit(1).then( docs => console.log(docs[0]._id) ). As does the other main point of the more cannonical and highly voted answer as now linked.

    – Neil Lunn
    Nov 23 '18 at 3:05













  • Or Photo.findOne({}).sort({ _id: 1 }).then( doc => console.log(doc) ) for that matter

    – Neil Lunn
    Nov 23 '18 at 3:09














  • 1





    You don't need to use callbacks, you can also make use of the Promise being returned from Mongoose.... but if you aren't familiar with either of those concepts then personally I'd advise you start with learning the basics of Node. Also, there are examples in the mongoose docs.

    – James
    Nov 22 '18 at 20:02













  • Don't believe what you read in the first post you find. From 2016 if the answer had any credibilty then it would have significantly more votes than 1. As the comment ays "read the documentation first". You can ask questions and read others answers once you see what the documentation has to say. And it says Photo.find({}).sort({ _id: 1 }).limit(1).then( docs => console.log(docs[0]._id) ). As does the other main point of the more cannonical and highly voted answer as now linked.

    – Neil Lunn
    Nov 23 '18 at 3:05













  • Or Photo.findOne({}).sort({ _id: 1 }).then( doc => console.log(doc) ) for that matter

    – Neil Lunn
    Nov 23 '18 at 3:09








1




1





You don't need to use callbacks, you can also make use of the Promise being returned from Mongoose.... but if you aren't familiar with either of those concepts then personally I'd advise you start with learning the basics of Node. Also, there are examples in the mongoose docs.

– James
Nov 22 '18 at 20:02







You don't need to use callbacks, you can also make use of the Promise being returned from Mongoose.... but if you aren't familiar with either of those concepts then personally I'd advise you start with learning the basics of Node. Also, there are examples in the mongoose docs.

– James
Nov 22 '18 at 20:02















Don't believe what you read in the first post you find. From 2016 if the answer had any credibilty then it would have significantly more votes than 1. As the comment ays "read the documentation first". You can ask questions and read others answers once you see what the documentation has to say. And it says Photo.find({}).sort({ _id: 1 }).limit(1).then( docs => console.log(docs[0]._id) ). As does the other main point of the more cannonical and highly voted answer as now linked.

– Neil Lunn
Nov 23 '18 at 3:05







Don't believe what you read in the first post you find. From 2016 if the answer had any credibilty then it would have significantly more votes than 1. As the comment ays "read the documentation first". You can ask questions and read others answers once you see what the documentation has to say. And it says Photo.find({}).sort({ _id: 1 }).limit(1).then( docs => console.log(docs[0]._id) ). As does the other main point of the more cannonical and highly voted answer as now linked.

– Neil Lunn
Nov 23 '18 at 3:05















Or Photo.findOne({}).sort({ _id: 1 }).then( doc => console.log(doc) ) for that matter

– Neil Lunn
Nov 23 '18 at 3:09





Or Photo.findOne({}).sort({ _id: 1 }).then( doc => console.log(doc) ) for that matter

– Neil Lunn
Nov 23 '18 at 3:09












1 Answer
1






active

oldest

votes


















0














You need to understand callbacks and Promises. But for your answer, you can take advantage of mongoose exec like this:



exports.next = function() {
return new Promise((resolve, reject) => {
Photo.find({}).sort({_id: 1}).limit(1).exec(function(err, photos) {
if (err) {
reject(err);
}
console.log('received: ', photos);
resolve();
}
}
}


Then in your router you should have:



getPhoto.next().then(function() {
res.redirect('/');
});


This is literally the basic usage of Promises in JS. There are lots of articles about usage of Promises and you can easily find them, and you can go to mongoose official page on how to query to mongo and get the results.






share|improve this answer
























  • From above comment "you can also make use of the Promise being returned from Mongoose.." - And when you look, you will see that the API allows a Promise to be returned without wrapping in a Promise yourself. So the comment that was there 4 hours before you answered was valid for you to read as well.

    – Neil Lunn
    Nov 23 '18 at 3:01


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to understand callbacks and Promises. But for your answer, you can take advantage of mongoose exec like this:



exports.next = function() {
return new Promise((resolve, reject) => {
Photo.find({}).sort({_id: 1}).limit(1).exec(function(err, photos) {
if (err) {
reject(err);
}
console.log('received: ', photos);
resolve();
}
}
}


Then in your router you should have:



getPhoto.next().then(function() {
res.redirect('/');
});


This is literally the basic usage of Promises in JS. There are lots of articles about usage of Promises and you can easily find them, and you can go to mongoose official page on how to query to mongo and get the results.






share|improve this answer
























  • From above comment "you can also make use of the Promise being returned from Mongoose.." - And when you look, you will see that the API allows a Promise to be returned without wrapping in a Promise yourself. So the comment that was there 4 hours before you answered was valid for you to read as well.

    – Neil Lunn
    Nov 23 '18 at 3:01
















0














You need to understand callbacks and Promises. But for your answer, you can take advantage of mongoose exec like this:



exports.next = function() {
return new Promise((resolve, reject) => {
Photo.find({}).sort({_id: 1}).limit(1).exec(function(err, photos) {
if (err) {
reject(err);
}
console.log('received: ', photos);
resolve();
}
}
}


Then in your router you should have:



getPhoto.next().then(function() {
res.redirect('/');
});


This is literally the basic usage of Promises in JS. There are lots of articles about usage of Promises and you can easily find them, and you can go to mongoose official page on how to query to mongo and get the results.






share|improve this answer
























  • From above comment "you can also make use of the Promise being returned from Mongoose.." - And when you look, you will see that the API allows a Promise to be returned without wrapping in a Promise yourself. So the comment that was there 4 hours before you answered was valid for you to read as well.

    – Neil Lunn
    Nov 23 '18 at 3:01














0












0








0







You need to understand callbacks and Promises. But for your answer, you can take advantage of mongoose exec like this:



exports.next = function() {
return new Promise((resolve, reject) => {
Photo.find({}).sort({_id: 1}).limit(1).exec(function(err, photos) {
if (err) {
reject(err);
}
console.log('received: ', photos);
resolve();
}
}
}


Then in your router you should have:



getPhoto.next().then(function() {
res.redirect('/');
});


This is literally the basic usage of Promises in JS. There are lots of articles about usage of Promises and you can easily find them, and you can go to mongoose official page on how to query to mongo and get the results.






share|improve this answer













You need to understand callbacks and Promises. But for your answer, you can take advantage of mongoose exec like this:



exports.next = function() {
return new Promise((resolve, reject) => {
Photo.find({}).sort({_id: 1}).limit(1).exec(function(err, photos) {
if (err) {
reject(err);
}
console.log('received: ', photos);
resolve();
}
}
}


Then in your router you should have:



getPhoto.next().then(function() {
res.redirect('/');
});


This is literally the basic usage of Promises in JS. There are lots of articles about usage of Promises and you can easily find them, and you can go to mongoose official page on how to query to mongo and get the results.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 0:30









imans77imans77

447413




447413













  • From above comment "you can also make use of the Promise being returned from Mongoose.." - And when you look, you will see that the API allows a Promise to be returned without wrapping in a Promise yourself. So the comment that was there 4 hours before you answered was valid for you to read as well.

    – Neil Lunn
    Nov 23 '18 at 3:01



















  • From above comment "you can also make use of the Promise being returned from Mongoose.." - And when you look, you will see that the API allows a Promise to be returned without wrapping in a Promise yourself. So the comment that was there 4 hours before you answered was valid for you to read as well.

    – Neil Lunn
    Nov 23 '18 at 3:01

















From above comment "you can also make use of the Promise being returned from Mongoose.." - And when you look, you will see that the API allows a Promise to be returned without wrapping in a Promise yourself. So the comment that was there 4 hours before you answered was valid for you to read as well.

– Neil Lunn
Nov 23 '18 at 3:01





From above comment "you can also make use of the Promise being returned from Mongoose.." - And when you look, you will see that the API allows a Promise to be returned without wrapping in a Promise yourself. So the comment that was there 4 hours before you answered was valid for you to read as well.

– Neil Lunn
Nov 23 '18 at 3:01





Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?