0
我在排序数据库时从最后到第一次遇到问题。这是我有:在nodejs和mongodb中排序数据库的问题
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.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>
但我得到一个错误!
谢谢你的帮助。
这是我的错误。 “错误:无法查找视图‘错误’的观点目录‘/ Web应用程序/ 12 /博客/ nodeblog /视图’” 我使用EJS作为我的模板呈现。 – jomeco1
我给出的代码抛出了那个错误?看起来像95号线上发生的错误。 –
抱歉打扰你。在第95道上,我拥有的是渲染错误页面。这是我的app.js:http://pastebin.com/CAQRrE0Q – jomeco1