0
我正在嘗試創建一個事件並將其存儲在我的mongo數據庫中。以下是我當前的事件模式。Node.js用mongo創建函數
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//blueprint for each new entry in the db
var eventSchema = new Schema({
eventName: {type:String},
eventDate: {type:String},
eventPlace:{type:String},
eventPrice: {type: Number}
});
module.exports = mongoose.model('Event', eventSchema);
這裏是我的user.js的我創建的功能代碼文件
// /route for new event
router.get('/newEvent', function (req,res,next) {
var messages = req.flash('error');
res.render('user/newEvent',{csrfToken: req.csrfToken(),messages: messages, hasErrors: messages.length >0});
});
//route for new event save
router.get('/createEvent', function(req, res, next) {
var event = new Event();
eventName = req.body.eventName;
eventDate = req.body.eventDate;
eventPlace = req.body.eventPlace;
eventPrice = req.body.eventPrice;
event.save(function(err) {
if(err) return next(err);
res.json({ message : 'Success!'});
});
});
這裏是我的表單創建我newEvent.hbs文件中的新事件。
<div class = "row">
<div class="col-md-4 col-md-offset-4">
<h1> Create a new Event</h1>
{{#if hasErrors}}
<div class=alert alert-danger">
{{# each messages}}
<p>{{this}}</p>
{{/each}}
</div>
{{/if}}
<form action="/user/newEvent" method="post">
<div class="form-group">
<label for="eventName">Event Name</label>
<input type="text" id="eventName" name="eventName"class="form-control">
</div>
<div class="form-group">
<label for="eventDate">Event Date</label>
<input type="text" id="eventDate" name="eventDate" class="form-control">
</div>
<div>
<label for="eventPlace">Place of Event</label>
<input type="text" id="eventPlace" name="eventPlace" class="form-control">
</div>
<div>
<label for="eventPrice">Price of Event: €</label>
<input type="text" id="eventPrice" name="eventPrice" class="form-control">
</div>
<input type="hidden" name="_csrf" value="{{ csrfToken}}">
<a href="createEvent" class="pull-right btn btn-success" role="button">Create Event</a>
</form>
</div>
到目前爲止,當我運行的代碼,我得到一個錯誤,指出事件名稱,eventPlace,EVENTDATE和eventPrice是必需的,還沒有被輸入,但是當我刪除「要求:真正的」從事件模式的新事件被創建,但沒有數據存儲在數據庫中。
的代碼,所以我試圖代碼和IM仍然得到發泄驗證的錯誤而失敗。 – Murf
我編輯的代碼,請嘗試使用這一個我通過對象在事件初始化這應該工作 –
所以現在錯誤已經消失,當我創建的事件,但沒有數據被存儲到數據庫 – Murf