如何从相同的命名输入构建并将数组推送到我的mongoose嵌入式模型

时间:2017-06-13 11:07:23

标签: mongoose ejs

我的模特:

<ImageView
    android:id="@+id/imagePowerlist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/powerlist"
    android:scaleType="centerInside"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:gravity="center" />

这是我的观点:

var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator');

var Trainschema = new mongoose.Schema({
    date1: {
        type: Date,
        default: Date.now
    },
    date2: {
        type: Date,
        default: Date.now
    },
    trainnum: {
        type: String,
        unique: true,
        required: true
    },
    startplace: String,
    destination: String,
    vagons: [{
        numvag: String,
        date1: {
            type: Date,
            default: Date.now
        },
        date2: {
            type: Date,
            default: Date.now
        },
        seatcount: String,
        reserves: [{
            numreserve: String,
            numseats: String
        }]
    }],
    weeks: [String],
    updated_at: {
        type: Date,
        default: Date.now
    },
});
Trainschema.plugin(uniqueValidator);

module.exports = mongoose.model('Train', Trainschema);

如您所见,我将从两对相同的命名输入中发送值:resnum和seatres。如何从收到的输入(resnum和seatres)建立一系列储备并将其推入Trainschema的储备属性?

这是一个控制器

<!DOCTYPE html>
<html>
  <head>
    <title>Create Train</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <div class="container">
      <h3><a href="/trains">Train List</a></h3>
      <h1>Create New Vagon</h1>
      <form action="/trains/upd/<%= train._id%>" method="post">
        <table>
          <tbody>
            <tr>
              <td>Train Number</td>
              <td><input type="text" name="trainnum" value="<%= train.trainnum%>"/></td>
            </tr>
            <tr>
              <td>From</td>
              <td><input type="date" name="date1" /></td>
            </tr>
            <tr>
              <td>To</td>
              <td><input type="date" name="date2" /></td>
            </tr>
            <tr>
              <td>Vagon number</td>
              <td><input type="text" name="numvag" /></td>
            </tr>
            <tr>
              <td>seats</td>
              <td><input type="text" name="seats" /></td>
            </tr>
            <tr>
              <td>Reserve number</td>
              <td><input type="text" name="resnum" /></td>
            </tr>
            <tr>
              <td>Seat Reserve</td>
              <td><input type="text" name="seatres" /></td>
            </tr>
            <tr>
              <td>Reserve number</td>
              <td><input type="text" name="resnum" /></td>
            </tr>
            <tr>
              <td>Seat Reserve</td>
              <td><input type="text" name="seatres" /></td>
            </tr>
            <tr>
              <td colspan="2"><button type="submit">Save</button></td>
            </tr>
          </tbody>
        </table>
      </form>
    </div>
    <script>
      
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

对多个字段使用相同的名称会给出一个具有该名称的数组,索引与订单匹配。

假设确保resnumseatres具有相同的长度,您可以使用地图功能创建reserves数组:

var reserves;
if(Array.isArray(req.body.reserves)){
    reserves = req.body.resnum.map(function(r, index) {
        return {
            numreserve: r,
            numseats: req.body.seatres[index]
        }
    })
}
else{
    reserves = [{
        numreserve: req.body.resnum, 
        numseats: req.body.seatres
    }];  //even it's a single object, this still needs to be put in array, given your schema
}

var vagon = {
    numvag: req.body.numvag,
    date1: req.body.date1,
    date2: req.body.date2,
    seatcount: req.body.seats,
    reserves: reserves
};