ejs 相关问题

“E”代表“有效”。 EJS是一种简单的模板语言,可让您使用纯JavaScript生成HTML标记。没有关于如何组织事物的宗教信仰。没有重新发明迭代和控制流。它只是简单的JavaScript。

通过 URL 将上下文传递到 NodeJS Express

我正在尝试使用 Node.js、Express 和 EJS 制作一个电子邮件验证系统。 目前,当用户注册时,服务器会生成一个链接,比如 https://www.website.com/verify=foo/ 然后...

回答 2 投票 0

客户端不显示Flash消息,如何解决?

我一直在关注这个 youtube 教程,我正在尝试弄清楚如何使用 connect-flash npm。这是我正在使用的代码: 服务器端: //应用程序.js const session = require('express-sessi...

回答 2 投票 0

CSS 和脚本文件未在 Express 应用程序上加载,给出 net::ERR_ABORTED 404(未找到)

我有一个快速后端服务器。当它启动时,它必须渲染包含index.js 的EJS 页面。 但是,我收到 3 个错误: 拒绝应用“http://localhost:3000/public/styles/style.css”中的样式

回答 1 投票 0

GET http://localhost:3000/public/index.js net::ERR_ABORTED 404(未找到)// MIME 类型

我有一个快速后端服务器。当它启动时,它必须渲染包含index.js 的EJS 页面。 但是,我收到 3 个错误: 拒绝应用“http://localhost:3000/public/styles/style.css”中的样式

回答 1 投票 0

如何从EJS获取表单数据?

我是这个话题的新手。我想了解更多有关 EJS 的信息。目标是在单击提交按钮后获取我的表单数据。 console.log 中的结果应该是我插入的数据...

回答 2 投票 0

节点路由间歇性工作

使用节点我在创建 2 条路由时遇到了麻烦。 有时 1 个有效,而另一个则无效。有时另一个有效,而第一个无效。有时两者都不起作用。 浏览器j...

回答 2 投票 0

无法更新MySQL中的条目

HTML/EJS: ... HTML/EJS: <div class="Edit-Panel" style="display: none;"> <div class="Edit-Wrapper"> <div class="Editing"> <p class="Edit-Header">Editing:</p> <p class="UserSelected">#usernamehere</p> </div> <div class="Edit-Inputs"> <div class="Input-Bar"> <input id="Edit-Username" class="Edit-Input" type="text" placeholder="Change Username"> <i class="fa-solid fa-user"></i> </div> <div class="Input-Bar"> <input id="Edit-Password" class="Edit-Input" type="text" placeholder="Change Password"> <i class="fa-solid fa-lock"></i> </div> <div class="Input-Bar"> <select name="User-Level" class="Drop-Down" id="Edit-Organization"> <option value="" disabled selected>Select Organization</option> <% organizations.forEach(org => { %> <option value="<%= org.organizationID %>"><%= org.organizationName %></option> <% }); %> </select> </div> <div class="Input-Bar"> <select name="Role" class="Drop-Down" id="Edit-Role"> <option value="" disabled selected>Edit Role</option> <option value="Admin">Admin</option> <option value="Teacher">Teacher</option> <option value="Student">Student</option> </select> </div> <button class="Edit-Btn" id="Submit-Edit"> Submit Changes </button> <button class="Edit-Btn" id="Close-Edit"> Cancel </button> </div> </div> </div> 相关前端JS: const userId = localStorage.getItem('selectedUserId'); const editUsernameInput = document.getElementById('Edit-Username'); const editPasswordInput = document.getElementById('Edit-Password'); const editOrganizationSelect = document.getElementById('Edit-Organization'); const editRoleSelect = document.getElementById('Edit-Role'); const submitEditBtn = document.getElementById('Submit-Edit'); let editedFields = {}; editUsernameInput.addEventListener('input', function () { editedFields.username = editUsernameInput.value; }); editPasswordInput.addEventListener('input', function () { editedFields.password = editPasswordInput.value; }); editOrganizationSelect.addEventListener('change', function () { const selectedOption = editOrganizationSelect.options[editOrganizationSelect.selectedIndex]; editedFields.organizationID = selectedOption.value; console.log('Organization ID:', editedFields.organizationID); }); editRoleSelect.addEventListener('change', function () { editedFields.role = editRoleSelect.value; }); submitEditBtn.addEventListener('click', function () { console.log(editedFields.organizationID); const requestBody = { userId, ...editedFields }; fetch('/api/useredit', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(requestBody) }) .then(response => { if (response.ok) { alert('Changes submitted successfully.'); editedFields = {}; editUsernameInput.value = ''; editPasswordInput.value = ''; editOrganizationSelect.selectedIndex = 0; editRoleSelect.selectedIndex = 0; } else { alert('Error submitting changes.'); } }) .catch(error => { alert('Error submitting changes:', error); }); }); 后端: const express = require('express'); const router = express.Router(); const DBconnection = require('../DB/Connector'); router.post('/useredit', (req, res) => { const { userId, username, password, organizationID, role } = req.body; console.log('Received Organization ID:', organizationID); let updateFields = []; let updateValues = []; if (username) { updateFields.push('UserName = ?'); updateValues.push(username); } if (password) { updateFields.push('UserPassword = ?'); updateValues.push(password); } if (role) { updateFields.push('UserRole = ?'); updateValues.push(role); } if (updateFields.length === 0) { return res.status(400).json({ error: 'No fields to update' }); } updateValues.push(userId); const updateUserQuery = `UPDATE useraccounts SET ${updateFields.join(', ')} WHERE UserID = ?`; DBconnection.query(updateUserQuery, updateValues, (err, updateUserResult) => { if (err) { console.error("Error updating user:", err); return res.status(500).json({ error: 'Error updating user' }); } if (organizationID) { const updateSectionLinkQuery = `UPDATE sectionlinks SET SectionID = ? WHERE UserID = ?`; const updateSectionLinkParams = [organizationID, userId]; DBconnection.query(updateSectionLinkQuery, updateSectionLinkParams, (err, updateSectionLinkResult) => { if (err) { console.error("Error updating sectionlink:", err); return res.status(500).json({ error: 'Error updating sectionlink' }); } console.log('Section Link Update Result:', updateSectionLinkResult); return res.status(200).json({ message: 'User updated successfully' }); }); } else { return res.status(200).json({ message: 'User updated successfully' }); } }); }); module.exports = router; 为什么我收到 POST http://localhost:3000/api/useredit 400(错误请求) 每当我尝试更改/更新给定的用户组织时。我可以更改用户、用户名、密码和角色。但是当我尝试更改组织时,我收到了错误的请求。 通过控制台日志,我已经验证在前端正在获取正确的组织 ID 并将其发送到后端,并且第一个控制台日志还返回正在接收正确的组织 ID。 但是所有其他日志都过去了 if (organizationID) 不返回任何内容,并且数据库不会发生任何更改。 感谢任何帮助,请不要太严厉。 总的来说,我对后端代码的处理方式做了一些改变,但保留了大部分以前的代码 const express = require('express'); const router = express.Router(); const DBconnection = require('../DB/Connector'); router.post('/useredit', (req, res) => { const { userId, username, password, organizationID, role } = req.body; console.log('Received Request Body:', req.body); // Initialize an array to store the promises for each update operation const updatePromises = []; // Update user details if provided if (username || password || role) { const updateFields = []; const updateValues = []; if (username) { updateFields.push('UserName = ?'); updateValues.push(username); } if (password) { updateFields.push('UserPassword = ?'); updateValues.push(password); } if (role) {a updateFields.push('UserRole = ?'); updateValues.push(role); } const updateUserQuery = `UPDATE useraccounts SET ${updateFields.join(', ')} WHERE UserID = ?`; const updateUserParams = [...updateValues, userId]; updatePromises.push(new Promise((resolve, reject) => { DBconnection.query(updateUserQuery, updateUserParams, (err, updateUserResult) => { if (err) { console.error("Error updating user:", err); reject('Error updating user'); } else { resolve('User details updated successfully'); } }); })); } // Update user's organization if provided if (organizationID !== undefineda) { const updateSectionLinkQuery = `UPDATE sectionlinks SET SectionID = ? WHERE UserID = ?`; const updateSectionLinkParams = [organizationID, userId]; updatePromises.push(new Promise((resolve, reject) => { DBconnection.query(updateSectionLinkQuery, updateSectionLinkParams, (err, updateSectionLinkResult) => { if (err) { console.error("Error updating sectionlink:", err); reject('Error updating sectionlink'); } else { resolve('User organization updated successfully'); } }); })); } // Execute all update operations asynchronously Promise.all(updatePromises) .then(messages => { res.status(200).json({ messages }); }) .catch(error => { res.status(500).json({ error }); }); }); module.exports = router;

回答 1 投票 0

event.preventDefault()影响代码的进一步执行

这个项目有3个.ejs页面index.ejs,loading.ejs和result.ejs。 index 首先加载,并在其脚本标记中包含以下代码: document.addEventListener("DOMContentLoaded", fu...

回答 1 投票 0

在ejs中为id添加增量

我正在开发一个使用 MVC 格式和 canJS 的 javascript 页面。在我的视图属性中,当我添加新元素时,我想添加 id 并为我加载的每个内容动态分配新 id。 代码在...

回答 2 投票 0

在ejs中给id添加增量器

我正在开发一个使用 MVC 格式和 canJS 的 javascript 页面。在我的视图属性中,当我添加新元素时,我想添加 id 并为我加载的每个内容动态分配新 id。 代码在...

回答 2 投票 0

如何使用express+ejs+chartjs将服务器端的对象以图表的形式显示到客户端?

我试图将两个对象权重(具有整数值的列表)和进度数据(具有字符串值的列表)从服务器端传递到客户端。在客户端,我陷入了如何渲染的困境......

回答 1 投票 0

尝试循环 JS 对象时出现“无效字符串长度”错误

我对编程非常陌生,现在我正在尝试构建我的第一个应用程序。这是一个日常开支跟踪器。我正在努力解决的部分是:当用户添加新项目(例如咖啡)和...

回答 1 投票 0

成功部署 Express 应用程序后,Vercel 显示内部服务器错误

我刚刚在 vercel 上部署了我的 Express 应用程序,该应用程序部署时没有任何错误,但现在当我转到 vercel 提供的链接时,我在浏览器上看到内部服务器错误。 我正在使用 EJS 模板

回答 1 投票 0

单击删除按钮后尝试从节点后端渲染我的 ejs 文件 - 不使用 ajax

我有一个简单的博客应用程序,使用node.js作为后端,使用ejs作为前端。我能够使用带有输入类型提交的网络表单来发布博客,调用发布路由。这很好用。但现在...

回答 1 投票 0

将.css文件添加到ejs

我正在使用 ejs 处理 Node.js(express),但无法将 .css 文件包含到其中。我单独尝试了与 html-css duo 相同的操作,效果很好...我该如何包含我的 .ejs 中也是如此...

回答 6 投票 0

Express:错误:无法在视图目录中查找视图“索引”

我将我的nodejs模板引擎设置为ejs。当我使用 ejs 模板运行 app.js 时,收到错误“无法在视图中查找视图‘错误’” 错误:无法在...中查找视图

回答 5 投票 0

EJS 未渲染

index.js 从“快递”进口快递; 从“body-parser”导入bodyParser; 常量应用程序 = Express(); 常量端口= 3000; app.set("视图引擎", "ejs"); 应用程序...

回答 1 投票 0

使用 .ejs 模板显示数据时,使用 Sequelize 的 hasOne 关联不起作用

我使用 ORM Sequelize 定义了 Customer 表和 Address 表之间的一对一关系 Customer.js的源码如下: // 一对一关系 // 客户....

回答 1 投票 0

Express.js 路由器下载 EJS 文件而不是显示它

所以,我正在使用纯 Node.js 创建实时视频共享/观看功能,而不使用任何框架。 然而,在开发进行到一半的时候,我遇到了一个问题......

回答 1 投票 0

node.js 中的“用户未定义”

我使用 EJS 作为我的模板引擎。我的浏览器说“users”未定义,但在我的 server.js 文件中,我将其定义为 var users = require("./routes/userws) 当我启动服务器并访问...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.