数据没有保存到mongoose / mongodb中

时间:2019-10-20 06:36:32

标签: javascript jquery node.js mongodb mongoose

我想将数据存储到猫鼬中,但是我不知道该怎么做: 我应该在控制器中做什么?

form:ejs:

<form action="#" id="" method="post" role="form" class="form-horizontal">
  <div class="form-body">
    <div class="form-group">
      <label class="col-md-3 control-label" for="form_control_1">Title
                                                    <span class="text-danger">*</span>
                                                </label>
      <div class="form-row">
        <div class="form-group col-md-4">
          <input type="text" class="form-control" data-required="1" id="firstName" name="title" value="">
        </div>
        <div class="form-group col-md-4" style="margin-left: 5px;">
          <input type="text" class="form-control" data-required="1" id="firstName" name="feedbackTitle" placeholder="feedback" value="">
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-3 control-label" for="form_control_1">Strategies
                                                    <span class="text-danger">*</span>
                                                </label>

      <div class="form-row">
        <div class="form-group col-md-4">
          <input type="text" class="form-control" data-required="1" id="firstName" name="strategies" value="">
        </div>
        <div class="form-group col-md-4" style="margin-left: 5px;">
          <input type="text" class="form-control" data-required="1" id="firstName" name="feedbackStrategies" placeholder="feedback" value="">
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-3 control-label" for="form_control_1">Evidance
                                                    <span class="text-danger">*</span>
                                                </label>

      <div class="form-row">
        <div class="form-group col-md-4">
          <input type="text" class="form-control" data-required="1" id="firstName" name="evidance" value="">
        </div>
        <div class="form-group col-md-4" style="margin-left: 5px;">
          <input type="file" class="form-control" data-required="1" id="firstName" name="feedbackEvidance" placeholder="feedback" value="">
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-3 control-label" for="form_control_1">Impact
                                                    <span class="text-danger">*</span>
                                                </label>

      <div class="form-row">
        <div class="form-group col-md-4">
          <input type="text" class="form-control" data-required="1" id="firstName" name="impact" value="">
        </div>
        <div class="form-group col-md-4" style="margin-left: 5px;">
          <input type="text" class="form-control" data-required="1" id="firstName" name="feedbackImpact" placeholder="feedback" value="">
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-3 control-label" for="form_control_1">CPD
                                                    <span class="text-danger">*</span>
                                                </label>

      <div class="form-row">
        <div class="form-group col-md-4">
          <input type="text" class="form-control" data-required="1" id="firstName" name="cpd" value="">
        </div>
        <div class="form-group col-md-4" style="margin-left: 5px;">
          <input type="text" class="form-control" data-required="1" id="firstName" name="feedbackCpd" placeholder="feedback" value="">
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-3 control-label" for="form_control_1">Invite Others
                                                    <span class="text-danger">*</span>
                                                </label>

      <div class="form-row">
        <div class="form-group col-md-8">
          <select class="form-control">
            <option>Teacher</option>
          </select>
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-3 control-label" for="form_control_1">
                                                    <span class="text-danger"></span>
                                                </label>

      <div class="form-row">
        <div class="form-group col-md-4">
          <a href="" class="btn btn-sm btn-primary m-l-md"> Apply for completion
                                                    </a>
        </div>
        <div class="form-group col-md-4" style="margin-left: 5px;">
          <a href="" class="btn btn-sm btn-primary m-l-md"> Accept completion
                                                    </a>
        </div>
      </div>
    </div>
  </div>
</form>

模式:

  var GoalSchema = new Schema({

  mentor: {type: Schema.ObjectId, ref: 'User'},
  title: {
    details: {type: String},
    feedback: {type: String},
  },
  explanation: {
    details: {type: String},
    feedback: {type: String},
  },
  strategies: {
    details: {type: String},
    feedback: {type: String},
  },
  evidance: [{
    description: {type: String},
    attachment : {type: Schema.ObjectId, ref: 'FileMeta'},
    feedback: {type: String},
    date: {type: Date},
  }],
  impact: {
    details: {type: String},
    feedback: {type: String},
  },
  cpd: {
    details: {type: String},
    feedback: {type: String},
  },
  otherTeachers:[{type: Schema.ObjectId, ref: 'User'}],
  createdBy: {type: Schema.ObjectId, ref: 'User'},
  last_reviewed: {type: Date, default: Date.now},
  evidance_date: {type: Date, default: Date.now},
  status: {type: String, enum: ['In_progress', 'Applied_completion', 'Completed'], default: 'In_process'},
  });

控制器:

   var saveGoal = function (req, res, next) {
        validate(req, res, next, function (title, strategies, explanation, evidance, impact, cpd) {
            if (!validationErr) {

                    var goal = new Goal;
                    goal.title = details,
                    goal.title = feedbackTitle,
                    goal.strategies = details,
                    goal.strategies = feedbackStrategies,
                    goal.explanation = details,
                    goal.explanation = feedbackExplanation,
                    goal.evidance = details,
                    goal.evidance = feedbackEvidance,
                    goal.impact = details,
                    goal.impact = feedbaclImpact,
                    goal.cpd = details,
                    goal.cpd = feedbackCpd,
                    goal.save(function (err, user) {
                        if (err) {
                            next(err);
                            return;
                        }
                            req.flash('info', 'Successfully saved the goal.');
                            res.redirect('/goal/create/' + req.body.goal);
                        });
                    });
                } 
        });
    };

  module.exports = function (app, portal, config) {
    app.use('/goal', router);


    var createGoal = (req, res) => {
        res.render("create_goal", {title:"Create Goal"});
    }

    var viewGoal = (req, res) => {
        res.render("view_goal", {title:"View Goal"});
    }

    var reportGoal = (req, res) => {
        res.render("report_goal", {title:"Report Goal"});
    }


    var viewAuthorize = portal.authorizeUser({accessType: 'view', feature: 'support', plugin: 'support'});
    var reviewAuthorize = portal.authorizeUser({accessType: 'review', feature: 'support', plugin: 'support'});

    router.get('/createGoal', viewAuthorize, createGoal);
    router.get('/viewGoal', viewAuthorize, viewGoal);
    router.get('/reportGoal', viewAuthorize, reportGoal);
    router.post('/create/new', create, saveGoal);

};

0 个答案:

没有答案