试图用控制器的事件填充FullCalender时间轴视图
使用EJS我可以显示日历并手动添加事件。最终,我将通过控制器从MongoDB获取事件。
// the controller
exports.getCalendar = async (req, res, next) => {
//const meetings = await Meeting.find();
const current_events = [
{
id: '1',
resourceId: 'MR2',
title: 'Meeting CEO',
start: '2019-08-05T11:00:00',
end: '2019-08-05T12:15:00'
},
{
id: '1',
resourceId: 'MR2',
title: 'Meeting CEO',
start: '2019-08-05T13:00:00',
end: '2019-08-05T13:15:00'
}
]
res.render('calendar', {
events: current_events,
});
}
// the router
const express = require('express');
const calendar = require('../controllers/calendar');
const router = express.Router();
router.get('/', calendar.getCalendar);
module.exports = router;
// app.js
const calendar = require('./routes/calendar');
app.use('/api/calendar', calendar);
// calendar script
document.addEventListener('DOMContentLoaded', function () {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
.....
events: events, //?????? page just hangs
.........
// console.log of events
[ { id: '1',
resourceId: 'MR2',
title: 'Meeting CEO',
start: '2019-08-06T11:00:00',
end: '2019-08-06T12:15:00' },
{ id: '1',
resourceId: 'MR2',
title: 'Meeting CEO',
start: '2019-08-06T13:00:00',
end: '2019-08-06T13:15:00' } ]
在HTML页面上,我添加了一个EJS行以查看事件。
<body>
<div id=calendar></div>
<p><%= events %></p>
</body>
这给出了[对象],[对象对象]
在日历脚本中手动添加事件作为数组工作正常,但我想显示控制器"事件"中导出的内容。它只是悬挂。任何指针都会非常奇怪。
是的杰西,遵循文档日历中的信息,事件作为json feed,我从mongo数据库中获取了数据
// events from the database
exports.eventsJSON = async (req, res, next) => {
const eventsFromDB = await Meeting.find();
res.json(eventsFromDB);
}
然后,我将其路由到URL say/eventsjsondata,然后添加 events: '/eventsJSONdata/',
在日历脚本中。希望这会有所帮助。