Javascript 如何从动态生成的资源中获取价值<;选项>&书信电报;选择>;快速ejs
首先,我感谢您阅读并尝试解决我的问题 我动态地呈现了我的{dropdown},但无法在后端获取所选的值 在我的表单上,我也得到了照片,套件的名称被动态渲染 如果我做了不正确的事情,请输入下面的代码 Lang-Javascript 快车 模板-EJS 我对body进行了解析,并将其设置为trueJavascript 如何从动态生成的资源中获取价值<;选项>&书信电报;选择>;快速ejs,javascript,node.js,express,visual-studio-code,ejs,Javascript,Node.js,Express,Visual Studio Code,Ejs,首先,我感谢您阅读并尝试解决我的问题 我动态地呈现了我的{dropdown},但无法在后端获取所选的值 在我的表单上,我也得到了照片,套件的名称被动态渲染 如果我做了不正确的事情,请输入下面的代码 Lang-Javascript 快车 模板-EJS 我对body进行了解析,并将其设置为true 问题我无法使用POST请求(未定义或null)将所选值获取到后端 但是如果我使用GET作为操作和请求,我确实会得到所选的值 再次感谢你。如果我不清楚,我会解释清楚。我是新加入的JS和EXPRESS节点
- 问题我无法使用POST请求(未定义或null)将所选值获取到后端
- 但是如果我使用GET作为操作和请求,我确实会得到所选的值
<form class="ui form" action="admin/uploadphoto" method="POST" enctype="multipart/form-data">
<h2 class=" ui dividing header" style="text-align: center;">Add Images to suites</h2>
<div class="two fields">
<div class="field">
<label>Add Suites Images</label>
<input name="suiteimage" type="file">
</div>
<div class="field">
<label>Suites*</label>
<select name="suiteselected" class="ui dropdown">
<% suites.forEach(function(suite) { %>
<option name = "suiteid" value="<%= suite.suite_name %>"><%= suite.suite_name %></option>
<% })%>
</select>
</div>
</div>
<input type="submit" class="ui button" tabindex="0" value="Save New Suite">
</form>
-路线
router.post ('/uploadphoto', service.PostPhoto);
``
我已经找到了解决这个问题的方法,应该需要对body进行解析,并将其扩展为true 将文本和图像作为一个表单发布到后端时,请确保
- 图像函数(上载)位于req、res函数内,或者值将为未定义或null
//my post for image and text function
const PostPhoto = (req, res) => {
const suite_photo_id = uuid();
upload(req, res, function(err) {
const {suiteselected} = req.body;
console.log(req.file);
var imagePath = req.file.path.replace(/^public\//, '');
console.log( suiteselected + " --ID")
console.log(imagePath);
pool.query(
"INSERT INTO suite_photos (suite_photo_id,suite_photo,suite_id) VALUES($1,$2,$3)",
[suite_photo_id, imagePath,suiteselected],
(error, result) => {
if (error) {
throw error;
} else{
console.log(result);
res.redirect("/admin");
}
}
);
});
};
这一切都改变了
谢谢大家 我已经找到了这个问题的解决方案,应该需要主体解析,并扩展为true 将文本和图像作为一个表单发布到后端时,请确保
- 图像函数(上载)位于req、res函数内,或者值将为未定义或null
//my post for image and text function
const PostPhoto = (req, res) => {
const suite_photo_id = uuid();
upload(req, res, function(err) {
const {suiteselected} = req.body;
console.log(req.file);
var imagePath = req.file.path.replace(/^public\//, '');
console.log( suiteselected + " --ID")
console.log(imagePath);
pool.query(
"INSERT INTO suite_photos (suite_photo_id,suite_photo,suite_id) VALUES($1,$2,$3)",
[suite_photo_id, imagePath,suiteselected],
(error, result) => {
if (error) {
throw error;
} else{
console.log(result);
res.redirect("/admin");
}
}
);
});
};
这一切都改变了
谢谢大家 你能修改你的帖子以包含函数uuid()吗?嗨@splaten,谢谢你的回复。我刚刚修改了它,但它是一个已安装的uuid();Package你能修改你的帖子以包含函数uuid()?Hi@splaten,谢谢你的回复。我刚刚修改了它,但它是一个已安装的uuid();包裹