0

Is it possible to query a returned collection in mongoose for mongodb. So in the example below I wish to return to the page the full list of steps, as well as the currentStep. The line I am unsure about is currentStep: steps.?????(page).

Thanks

var page = (req.params.page == undefined?1:req.params.page)
db.stepModel.find().sort({number:1}).exec(function(err, steps) {
 if (err) { return next(err); }
 res.render('scheme', { 
   title: 'Fnol',
   user: req.user, 
   steps:steps,
   currentStep: steps.?????(page), 
   page:page
 });
};

1 Answer 1

1

You can use steps as an array:

currentStep : steps[page - 1].toObject()

The - 1 is because it seems that you use 1-based indexing for the page, whereas arrays are 0-based.

The toObject() converts the Mongoose result into a proper JS object, so you can access properties like _id from within your template.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.