[英]trying to pass check boxes as string (node)
我正在做一个小项目来学习全栈(mongo&node&express&ejs),我在项目中有什么:-object-name film。 它有几个字段,比如长度、流派、导演。 - 添加电影形式。
我将对象存储在 mongo 中。 总的来说一切都很好。 在添加电影中,类型 label 是一个选项 - 我决定将其设置为复选框会更专业,因此用户可以根据需要选择多少类型(毕竟大多数电影都属于几种类型。 ) 我需要以下两个选项之一: - 将 object 的流派字段类型从字符串更改为数组,并存储所有 select 复选框的数组,并在电影页面中显示它们。 (我没有设法实现所有选定复选框名称的数组) - 将流派字段类型保留为字符串,然后将其显示为字符串。 (到目前为止没有工作,变得空白)
现在,当它只是选项时,它作为字符串传递,并且一切正常且非常简单。 但到目前为止,我无法使用复选框。 尝试作为数组和字符串,到目前为止所有错误或未定义。 我看了很多解决方案,我看到的只是 php 中的解决方案或需要文档属性的解决方案(这不起作用,从谷歌搜索我理解它是因为节点。)
知道如何实现这样的事情吗? 这就是我现在所拥有的(作为选项的类型,及其工作):
const filmSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
releaseYear: {
type: Number,
required: true
},
genre: {
type: String,
required: true
},
#表单流派部分
<label>Genre</label>
<select name="genre">
<% genre.forEach(g => { %>
<% if (g === film.genre) { %>
<option selected label="<%= g %>" value="<%= g %>"></option>
<% } else { %>
<option value="<%= g %>"><%= g %></option>
<% } %>
<% }) %>
</select>
router.post('/', async (req, res) => {
const film = new Film({
title: req.body.title,
director: req.body.director,
releaseYear: req.body.releaseYear,
genre: req.body.genre,
length: req.body.length,
description: req.body.description,
})
<h2> <%= film.title %> </h2>
<img height="150" width="100" src="<%= film.posterImagePath %>">
<div>Director: <%= film.director.name %></div>
<div>Release Year: <%= film.releaseYear %> </div>
<div>Genre: <%= film.genre %> </div>
<div>Length: <%= film.length %></div>
<div>Description: <%= film.description %></div>
我在这个问题上停留了几个小时,任何帮助将不胜感激!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.