javascript - 如何使用表单输出制作数组并使用 express 发布到 mongodb
问题描述
我有这个猫鼬模式
const AsientoSchema = new Schema({
numero_asiento: Number,
fecha: Date,
cuentas: {type:Array,"default":[]},
// name: { type: Schema.ObjectId, ref: "Cuenta" }
// type: String,
// type: [{ type: Schema.Types.ObjectId, ref: 'counts' }]
})
我有两个 HTML5 表单,第一个是将这些值发布到“asiento”猫鼬模式,第二个表单创建一个“cuenta”
<h1>crear asiento</h1>
<form action="/crear-asiento" method="post" class="asientoForm">
<strong>Fecha: </strong>
<input type="date" name="fecha" required>
<br>
<br>
<button type="submit" class="asientoButton">Añadir asiento</button>
</form>
<form action="/crear-asiento" method="post" class="cuentasForm">
<strong>Elija cuenta: </strong>
<select name="nombrecuenta" id="">
<% for(var i=0; i < asientos.length; i++) { %>
<option value="<%= asientos[i].name %>"><%= asientos[i].name %></option>
<% } %>
</select>
<input type="radio" name="dh" value="debe" checked> Debe
<input type="radio" name="dh" value="haber"> Haber
<br>
<br>
<strong>Importe: </strong>
<input type="number" name="importe">
<button type="submit" class="cuentaButton" >Añadir cuenta</button>
</form>
我有这个快递
const express = require('express');
const router = new express.Router();
const Asiento = require('../model/asientos');
const Count = require('../model/counts')
router.get('/crear-asiento', async (req, res) => {
const asientos = await Count.find();//.populate("name");
const nvo_asiento = await Asiento.find();//.populate("name");
res.render('crear-asiento', {
asientos, nvo_asiento
});
});
router.post('/crear-asiento', async (req, res, next) => {
const nvo_asiento = new Asiento(req.body);
await nvo_asiento.save(); //para que se almacene en la db con async await
res.redirect('/crear-asiento'); // redirecciona a la ruta inicial del servidor
});
module.exports = router
所以我想用表单创建多个“cuenta”对象,并插入“cuentas”数组我该怎么做?我已经尝试将它们存储到一个带有 js 函数的数组中,在 ejs 文件中或在 express 中,但没有任何效果..
我开始用 express 和 mongodb 编码,也许我没有意识到一些基本的东西
解决方案
推荐阅读
- kubernetes - 暂时停止 K8s 和 Cassandra 集群
- javascript - 尝试将待办事项呈现为
- ,但 todo.name 未定义
- docker - 使用 ingress-nginx 在 Kubernetes 中提供静态资产
- python - 确实网站的Scrapy爬虫分页
- python - 在 Python 中实现 RC4
- algorithm - 如何计算以下代码的时间函数 T(n)?
- intellij-idea - IntelliJ 环境变量
- typescript - React-native-reanimated 在网络中不起作用
- performance-testing - Loadrunner (VUGen) 是否支持两因素身份验证?
- html - Bootstrap 将复选框放入 3 个均匀间隔的列中,并在表单中居中