I'm having trouble sorting my database from last to first. This is what I have:
Index.js
var express = require('express');
var router = express.Router();
var mongo = require("mongodb");
var db = require("monk")("localhost/nodeblog");
router.get('/', function(req, res, next) {
var db = req.db;
var posts = db.get('posts');
posts.find({},{}, function(err, posts){
res.render("index", {posts: posts})
}).sort({_id: -1}).exec(function(err, docs){
if(err) throw err
console.log(docs)
})
});
Index.ejs
<% include ./layout/header %>
<% posts.sort({$natural:-1}).forEach(function(x) { %>
<h1 class="ui header"><a href="/post/show/<%= x._id %>"><%= x.title %></a>
<div class="sub header">Posted in <%= x.categories %> by <%=x.author%> on <%= moment(x.date).format("MM-DD-YYYY")%></div>
</h1>
<h4 clas="ui header">
<%= x.body %>
</h4>
<div class="ui teal button"><a href="/post/show/<%= x._id %>"> Read More </a></div>
But I get an error!
Thank you for your help.
via jomeco1
No comments:
Post a Comment