Thursday, 21 December 2017

node.js sorting object (mongoose, express, ejs)

/* SOLVED */i have this problem sorting my object of data in my index of my blog app. I have a blog app based on Express using ejs and mongoDB using mongoose. What i want is sorting the results so the newest post starts at the top. At this moment it will show the first post at the top.app.js / mongoose schemablogSchema = new mongoose.Schema({ title: String, image: String, body: String, created: {type: Date, default: Date.now} }); var Blog = mongoose.model("Blog", blogSchema);app.js / Index routeapp.get("/blogs", (req, res)=>{ Blog.find({}, (err, blogs)=>{ if(err){ console.log("Error!"); console.log(err); } else { res.render("index", {blogs: blogs}); } }); });index.ejs foreach<% blogs.forEach(function(blog){ %> img <%= blog.title %> <%= blog.created.toDateString() %>

<%- blog.body.substring(0, 200) %>...

Read More <% }) %>Does anyone have a clue how i can do this?I also have this posted on stack overflow where its easier to read the code: http://ift.tt/2BpEdxf

Submitted December 21, 2017 at 08:07AM by bjelke2

No comments:

Post a Comment