我正在尝试将mongoose findOne
方法与我的日期模型属性一起使用,以仅使用我最新文档中的数据。我正在使用EJS模板系统,并试图在我的视图中使用此方法,但收到错误,} has no method 'findOne' at my
events.findOne`行。我该如何使用这种方法?它应该在routes.js中调用吗?
eventsModel.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var EventSchema = new Schema({
name: String,
title: String,
company: String,
url: String,
date: { type: Date, default: Date.now },
upcomingEvent: { type: Boolean, default: false }
});
module.exports = mongoose.model('Event', EventSchema);
routes.js
var express = require('express');
var router = express.Router();
var Event = require('./models/eventsModel');
//GET '/'
router.get('/', function(req, res){
res.render('pages/index.ejs');
});
//GET '/about'
router.get('/about', function(req, res){
res.render('pages/about.ejs');
});
//GET '/media'
router.get('/media', function(req, res){
res.render('pages/media.ejs');
});
router.route('/admin/events/create')
.post(function(req, res){
var events = new Event();
events.name = req.body.name;
events.title = req.body.title;
events.company = req.body.company;
events.url = req.body.url;
events.upcomingEvent = req.body.upcomingEvent;
events.save(function(err){
if (err)
res.send(err);
res.redirect(303, '/events');
});
})
.get(function(req, res){
Event.find(function(err, events){
if (err)
res.send(err);
res.render('pages/events-create.ejs');
});
});
router.route('/admin/events/:events_id')
.get(function(req, res){
Event.findById(req.params.events_id, function(err, events){
if (err)
res.send(err);
res.json(events);
});
})
.put(function(req, res){
Event.findById(req.params.events_id, function(err, events){
if (err)
res.send(err);
res.json({ message: 'Event Updated'});
});
});
//GET '/events'
router.get('/events', function(req, res){
Event.find(function(err, events){
if (err)
res.send(err);
events.name = req.body.name;
events.title = req.body.title;
events.company = req.body.company;
events.url = req.body.url;
events.upcomingEvent = req.body.upcomingEvent;
res.render('pages/events.ejs', {
events : events
});
});
});
module.exports = router; //export all objects in router module
events.ejs
<!DOCTYPE HTML>
<html>
<head>
<% include ../partials/head %>
</head>
<body>
<% include ../partials/navigation %>
<div class="grid" id="lvc-events-title-container">
<div class="col-1-1">
<div id="events-heading">
<h1><i>-EVENTS</i></h1>
</div>
</div>
</div>
<div class="grid" id="lvc-current-event-container">
<div class="col-1-1">
<div id="current-event">
<% events.findOne({'events.date': -1}, function(events) { %>
<h2><i>-UPCOMING SPEAKER</i></h2>
<div id="current-event-image">
<img src="http://placehold.it/140x100" id="speaker-image">
<h3>John Doe</h3>
<p>Title</p>
<p>Company</p>
<% }); %>
</div>
</div>
</div>
</div>
<div class="grid" id="lvc-past-events-container">
<div class="col-1-1">
<div id="past-speakers">
<h2><i>-PAST SPEAKERS</i></h2>
<% events.forEach(function(events) { %>
<ul>
<li><a href="<%= events.url %>"><%= events.name %></a> (<%= events.title %> | <%= events.company %>)</li>
</ul>
<% }); %>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
我没有非常广泛地使用过Mongoose,但是当你将'events'数组传递给你的模板时,你已经隐含地传递了最新的事件(加上之前的事件) - 这意味着你只需按摩数据了一点点以期望的方式显示它。注意:all方法可能会为您提供更好的服务,因为它看起来不像您对事件进行任何过滤。
此代码可能无效,并假设events数组中的数据按日期排序(将其视为伪代码),但它应该让您在正确的轨道上。
<!DOCTYPE HTML>
<html>
<head>
<% include ../partials/head %>
</head>
<body>
<% include ../partials/navigation %>
<div class="grid" id="lvc-events-title-container">
<div class="col-1-1">
<div id="events-heading">
<h1><i>-EVENTS</i></h1>
</div>
</div>
</div>
<div class="grid" id="lvc-current-event-container">
<div class="col-1-1">
<div id="current-event">
<h2><i>-UPCOMING SPEAKER</i></h2>
<div id="current-event-image">
<img src="http://placehold.it/140x100" id="speaker-image">
<h3><%= events[0].name %></h3>
<p><%= events[0].title %></p>
<p><%= events[0].company %></p>
</div>
</div>
</div>
</div>
<div class="grid" id="lvc-past-events-container">
<div class="col-1-1">
<div id="past-speakers">
<h2><i>-PAST SPEAKERS</i></h2>
<% for (var i = 1; i < events.length; i++) { %>
<ul>
<li><a href="<%= events[i].url %>"><%= events[i].name %></a> (<%= events[i].title %> | <%= events[i].company %>)</li>
</ul>
<% }; %>
</div>
</div>
</div>
答案 1 :(得分:0)
好吧,我能说的几句小事......
findOne
是模型上的static
方法(如find Event.find
),而不是文档上的instance
方法(如save {{1} }})event.save
方法路由器:
find
查看
router.get('/events', function(req, res) {
Event.find({})
.sort({'date': -1})
.exec(function(err, events) {
if (err)
res.send(err);
// Events is an array of event models, so this is setting properties on an array
// It also doesn't look like you are using it in your view so its not needed
//events.name = req.body.name;
//events.title = req.body.title;
//events.company = req.body.company;
//events.url = req.body.url;
//events.upcomingEvent = req.body.upcomingEvent;
// The events local in your page will be an array with index[0] being the most recent event, which I believe is your 'UPCOMING SPEAKER'. You can then loop over the rest of the array from 1.
res.render('pages/events.ejs', {
events : events
});
});
});
...
<div class="grid" id="lvc-current-event-container">
<div class="col-1-1">
<div id="current-event">
<h2><i>-UPCOMING SPEAKER</i></h2>
<div id="current-event-image">
<img src="http://placehold.it/140x100" id="speaker-image">
<h3><%= events[0].name %></h3>
<p><%= events[0].title %></p>
<p><%= events[0].compant %></p>
</div>
</div>
</div>
</div>