无法POST显示网址两次

时间:2018-04-19 02:43:40

标签: node.js express post routes ejs

当我发布帖子请求时,我收到错误。我看了两次帖子请求。

此外,我遇到了表单问题,我正在尝试在(员工集合)中嵌套集合(qcerror)。

错误:(邮件请求网址重复)。

Cannot POST /employee/5ad7cf11729b4c08ae721602/qcerror/employee/5ad7cf11729b4c08ae721602/qcerror

代码:

app.js

var express =require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var flash = require("connect-flash");
var authRoute = require("./routes/auth.js");
var formsRoute = require("./routes/forms.js");
var employeesRoute = require("./routes/employees.js");
var commentsRoute = require("./routes/comments.js");
var callinsRoute = require("./routes/callins.js");
var qcerrorsRoute = require("./routes/qcerrors.js");
var safetyincidentsRoute = require("./routes/safetyincidents.js");


// Configuring Passport
var User = require('./models/user.js');
var passport = require('passport');
var localStrategy = require('passport-local');
var expressSession = require('express-session');

// DB Config
var mongoose = require('mongoose');
mongoose.connect('xxxxxxxxxx');
//'mongodb://localhost/xxxxxx'
//'mongodb://xxxxxx'

// Config ejs + bodyParser + methodOverride
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
app.use(flash());
app.use(express.static(__dirname + "/public"));


// Configuring Passport
app.use(expressSession({
    secret: "secretkey",
    resave: false,
    saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate));
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use(function(req, res, next){
    res.locals.currentUser = req.user;
    next();
});

app.use(authRoute);
app.use(formsRoute);
app.use(employeesRoute);
app.use(commentsRoute);
app.use(callinsRoute);
app.use(qcerrorsRoute);
app.use(safetyincidentsRoute);


//==============================
// SERVER START
//==============================

app.listen(process.env.PORT, process.env.IP, function(){
    console.log("SERVER START");
});

路线:

//Post New QC Error Route
router.post("/employee/:id/qcerror", isLoggedin, function(req, res){
  //lookup campground using ID
  Employee.findById(req.params.id, function(err, employee){
      if(err){
          console.log(err);
          res.redirect('/employee/' + employee._id);
      } else {
        QcError.create(req.body.qcerror, function(err, qcerror){
          if(err){
              console.log(err);
          } else {
              //add username and id to comment
              qcerror.author.id = req.user._id;
              qcerror.author.username = req.user.username;
              //save comment
              qcerror.save();
              employee.qcerrors.push(qcerror);
              employee.save();
              console.log(qcerror);
              res.redirect('/employee/' + employee._id);
          }
        });
      }
  });
});

形式:

 <form class="ui tiny form" action="employee/<%= employee._id%>/qcerror" method="POST" enctype="multipart/form-data">

  <p></p>

  <div class="ui stackable grid">

    <div class="four wide column">
      <div class="required field">
        <label>Date / Time</label>
        <input type="datetime-local" name=qcerror[datetime] placeholder="Date/Time" required>
      </div>
      <div class="required field">
        <label>Internal or External Error</label>
        <select class="ui fluid dropdown" type="text" name=qcerror[internalexternal] placeholder="Internal or External" required>
          <option value="None"></option>
          <option value="External">External</option>
          <option value="Internal">Internal</option>
        </select>
      </div>
      <div class="required field">
        <label>Reference</label>
        <input type="text" name=qcerror[referenceid] placeholder="Reference" style='text-transform: uppercase' required>
      </div>
    </div>
    <div class="four wide column">
      <div class="required field">
        <label>SKU</label>
        <input type="text" name=qcerror[sku] placeholder="SKU" style='text-transform: uppercase' required>
      </div>
      <div class="required field">
        <label>Pick Quantity</label>
        <input type="number" name=qcerror[pickquantity] placeholder="Pick Quantity" style='text-transform: none'>
      </div>
      <div class="required field">
        <label>Actual Quantity</label>
        <input type="number" name=qcerror[actualquantity] placeholder="Actual Quantity" style='text-transform: none'>
      </div>
      <div class="required field">
        <label>Location</label>
        <input type="text" name=qcerror[location] placeholder="Location" style='text-transform: uppercase' required>
      </div>
    </div>
    <div class="four wide column">
      <div class="required field">
        <label>Description</label>
        <textarea type="text" name=qcerror[description] placeholder="Description" style='text-transform: none' rows="9" required></textarea>
      </div>
    </div>  
    <div class="four wide column">
      <div class="required field">
        <label>Shift</label>
        <select class="ui dropdown" type="number" name=qcerror[shift] placeholder="Shift" required>
          <option value="none"></option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
      </div>  
      <div class="required field">
        <label>Department</label>
        <select class="ui dropdown" type="text" name=qcerror[department] placeholder="Department" required>
          <option value="none"></option>
          <option value="Carton-Pack">Carton-Pack</option>
          <option value="Equipment">Equipment</option>
          <option value="Inventory">Inventory</option>
          <option value="Pallet-Pack">Pallet-Pack</option>
          <option value="Picking">Picking</option>
          <option value="Receiving">Receiving</option>
        </select>
      </div>
      <div class="required field">
        <label>UserID</label>
        <input type="text" name=qcerror[employeeid] placeholder="UserID" style='text-transform: uppercase' required>
      </div>
      <div class="required field">
        <label>Auditor UserID</label>
        <input type="text" name=qcerror[auditorid] placeholder="Auditor UserID" style='text-transform: uppercase' required>
      </div>
    </div>

  </div>

  <p></p>

  <button onclick="myFunction()" class="ui fluid  button" type="submit" name="submit" value="submit">Submit</button>
  <script>
  function myFunction() {
      alert("Form Submitted!");
  }
  </script>

</form>

0 个答案:

没有答案