How to pass an object from EJS to Node.js Router - Javascript, EJS, Node.js, MySQL - Stack Overflow

I am working on looping through records in a MySQL table, displaying them in an EJS file, and when one

I am working on looping through records in a MySQL table, displaying them in an EJS file, and when one is clicked on, routing to a page to edit the record. However, I am having trouble passing the single Object back to my routes.js file to route it to the edit page. I have had no problem looping through all the records and printing them, so I know the data is there. Here is the part of the EJS file where I am outputting all of the records for user selection:

<body>
<div class="container">

<div class="col-sm-6 col-sm-offset-3">

    <h1><span class="fa fa-chess"></span> View Roommate Agreements</h1><br>
    <p align="center">Click on a Roommate Agreement below to view the full response and edit if necessary.</p><br>

    <% for (var i=0; i < Agreements.length; i++) { %>
        <a href="/editAgreement" style="text-decoration: none; color: #333333">
            <div class="well" align="center">

                <h3>Roommate Agreement for Room #<%= Agreements[i].roomNumber%></h3>
                <p><%= Agreements[i].roommate1%></p>
                <p><%= Agreements[i].roommate2%></p>
                <% var Agreement = Agreements[i]%> //THIS is where I'm trying to declare the object
            </div>
        </a>
    <% } %>

    <hr>
    <p align="center">
        <a href="/agreement" class="btn btn-default btn-sm"><span class="fa fa-arrow-circle-left"></span> Back</a>
        <a href="/logout" class="btn btn-default btn-sm"><span class="fa fa-sign-out-alt"></span> Logout</a>
    </p>

</div>

</div>
</body>

And here is the Get function to route the object to the edit page. You can see where I'm trying to log the object to the console to make sure I'm getting the right information, but it's currently printing as "undefined".

app.get('/editAgreement', isLoggedIn, function(req, res) {
    res.render('editAgreement.ejs', {
        Employee: req.user
    })
    console.log(req.body.Agreement);
})

I'm not sure if the problem is how I'm declaring the object variable in EJS or how I'm trying to use it in the Get function, as I am new to Javascript. Any tips or help would be appreciated, thanks! :)

I am working on looping through records in a MySQL table, displaying them in an EJS file, and when one is clicked on, routing to a page to edit the record. However, I am having trouble passing the single Object back to my routes.js file to route it to the edit page. I have had no problem looping through all the records and printing them, so I know the data is there. Here is the part of the EJS file where I am outputting all of the records for user selection:

<body>
<div class="container">

<div class="col-sm-6 col-sm-offset-3">

    <h1><span class="fa fa-chess"></span> View Roommate Agreements</h1><br>
    <p align="center">Click on a Roommate Agreement below to view the full response and edit if necessary.</p><br>

    <% for (var i=0; i < Agreements.length; i++) { %>
        <a href="/editAgreement" style="text-decoration: none; color: #333333">
            <div class="well" align="center">

                <h3>Roommate Agreement for Room #<%= Agreements[i].roomNumber%></h3>
                <p><%= Agreements[i].roommate1%></p>
                <p><%= Agreements[i].roommate2%></p>
                <% var Agreement = Agreements[i]%> //THIS is where I'm trying to declare the object
            </div>
        </a>
    <% } %>

    <hr>
    <p align="center">
        <a href="/agreement" class="btn btn-default btn-sm"><span class="fa fa-arrow-circle-left"></span> Back</a>
        <a href="/logout" class="btn btn-default btn-sm"><span class="fa fa-sign-out-alt"></span> Logout</a>
    </p>

</div>

</div>
</body>

And here is the Get function to route the object to the edit page. You can see where I'm trying to log the object to the console to make sure I'm getting the right information, but it's currently printing as "undefined".

app.get('/editAgreement', isLoggedIn, function(req, res) {
    res.render('editAgreement.ejs', {
        Employee: req.user
    })
    console.log(req.body.Agreement);
})

I'm not sure if the problem is how I'm declaring the object variable in EJS or how I'm trying to use it in the Get function, as I am new to Javascript. Any tips or help would be appreciated, thanks! :)

Share Improve this question asked Mar 29, 2018 at 1:36 Blake LewisBlake Lewis 1133 silver badges11 bronze badges 6
  • Are you trying to create unique links to each agreement? (Which contain a little extra data like the roommates' names for context?) – Stephen Gheysens Commented Mar 29, 2018 at 1:44
  • Blake, in order to access the Agreements variable in the template, you need to first pass it in from the render call, just like how you passed in Employee. – Stephen Gheysens Commented Mar 29, 2018 at 1:52
  • Please post your get function in client-side – Shimon Brandsdorfer Commented Mar 29, 2018 at 1:54
  • @StephenGheysens Once a specific agreement is clicked on, I want it to redirect to a page where the user can edit it. I know I will need to pass it from the render call, but I'm still working on getting the data there. That console.log function currently prints undefined in the console, which it wouldn't if the data was being passed correctly. – Blake Lewis Commented Mar 29, 2018 at 1:56
  • @ShimonBrandsdorfer I'm not quite sure what you mean. Can you clarify? – Blake Lewis Commented Mar 29, 2018 at 1:57
 |  Show 1 more ment

2 Answers 2

Reset to default 5

to passe variables to another page using GET you have to passe them in the url, with that you will expose you data and that's not safe and a lot of work if you have a lot of information to pass,you should passe only the id of the agreement to the get route, fetch the agreement and then render the view, like :

<% for (var i=0; i < Agreements.length; i++) { %>
    <a href="/editAgreement/<%= Agreements[i].id %>" style="text ...

and then :

app.get('/editAgreement/:id', isLoggedIn, function(req, res) {
    let agreementId = req.params.id;

    let agreement = // go fetch the agreement from the database like you did before with the list of agreements

    res.render('editAgreement.ejs', agreement)
})

If you want to get the Object sent along with your GET request, you need to send them along, and it doesn't seem from your code that you are doing it.

The quickest solution would be to use the querystring module. By requirting it on the top of your code, and then use it to send the agreement Object, as follows:

<% 

 const querystring = require('querystring');
 for (var i=0; i < Agreements.length; i++) { 
       var Agreement = Agreements[i]
 %>
        <a href="/editAgreement/?<%= querystring.stringify(Agreement) %>" style="text-decoration: none; color: #333333">
            <div class="well" align="center">

                <h3>Roommate Agreement for Room #<%= Agreement.roomNumber%></h3>
                <p><%= Agreement.roommate1%></p>
                <p><%= Agreement.roommate2%></p>
            </div>
        </a>
    <% } %>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745327993a4622747.html

相关推荐

  • 如何使用python查询Prometheus监控数据

    一.环境准备软件库包版本python3.8prometheus-api-client0.5.7二.安装步骤代码语言:python代码运行次数:0运行复制pip3 install prometheus-api-client默认安装最新版本的p

    1小时前
    20
  • windows 配置 upx

    ​1、下载:2、解压:解压后​3、配置环境变量,右键我的电脑-——》属性——》高级属性:在Path中添加: D:ProgramFileupx-5.0.0-win644、验证cmd中输入:代码语言:txt复制upx --version

    1小时前
    20
  • 谷歌云第三方SSH工具登录与一键重装系统

    一&#xff0c;设置 root 密码 先选择从浏览器打开 ssh 连接服务器切换到 root 账号&#xff0c;输入代码&#xff1a;sudo -i设置 root 密码&#xff0c;输入代码&

    1小时前
    00
  • 文章降 AI 痕迹方法与工具速览

    文章降AI的方法和工具汇总‌如下: 这几天又认真研究类了一下,想让 AI 生成的文章更自然,摆脱程式化痕迹,可尝试以下方法。借助 GPT、文字滚筒鸭,朱雀大模型检测器、豆包、kimi 等大模型,输入文本后,它们能通过调整结构、替换同义词等操

    1小时前
    00
  • MySQL 8.4 配置SSL组复制(八个步骤)

    环境这里有三台MySQL主机,分别是192.168.3.71,72,73,主机名分别对应71.3_mgr1,72.3_mgr2,73.3_mgr3,操作系统均为Oracle Linux 8.10 X64,MySQL版本均为MySQL 8.4

    1小时前
    00
  • MySQL8.4 Enterprise安装Firewall及测试

    参考:.4enfirewall.html1.首先执行安装SQL,路径在baseshare目录下代码语言:javascript代码运行次数:0运行复制cd u01mysql3308baseshare[root@mysql8_3

    1小时前
    00
  • AI生态暗战升级,科技巨头铁幕下的终极博弈

    一场围绕AI与智能体的标准、协议及生态的暗战已然蓄势待发。在美剧《权力的游戏》中,不到终局,主角归属始终成谜。如今的AI行业,正上演着同样扣人心弦的戏码。这并非是传统意义上的军事或政治博弈,而是一场围绕AI与智能体的标准、协议及生态展开的暗

    1小时前
    00
  • 取消Win10开机系统选择倒计时,让电脑秒进系统

    取消Win10开机系统选择倒计时,让电脑秒进系统 近期,不少Win10用户反映在开机时会遇到一个选择系统的倒计时画面,这在一定程度上延缓了开机进程。对于追求高效启动体验的用户来说,这无疑是一个不必要的步骤。那么,如何取消这个倒计时,让电脑

    1小时前
    00
  • 2025年最受欢迎的10款免费CRM软件大对比

    在数字化转型浪潮下,越来越多的企业开始重视客户关系管理(CRM)系统。一个高效的CRM不仅能帮助企业理清客户脉络,还能提升销售效率、优化服务体验。2025年,市场上涌现了众多优秀的免费CRM软件,本文将为大家对比10款最受欢迎的产品,助您选

    1小时前
    00
  • PyMC+AI提示词贝叶斯项目反应IRT理论Rasch分析篮球比赛官方数据:球员能力与位置层级结构研究

    全文链接:tecdat?p=41666在体育数据分析领域不断发展的当下,数据科学家们致力于挖掘数据背后的深层价值,为各行业提供更具洞察力的决策依据。近期,我们团队完成了一项极具意义的咨询项目,旨在通过先进的数据分析手段,深入探究篮球比赛中

    1小时前
    00
  • 初始JESD204B高速接口协议(JESD204B一)

    01、对比LVDS与JESD204JESD204B是逻辑器件和高速ADCDAC通信的一个串行接口协议,在此之前,ADCDAC与逻辑器件交互的接口大致分为如下几种。低速串行接口(I2C、SPI)、低速并行接口(包含时钟信号和并行数据信号,

    51分钟前
    00
  • 如何打造高效AI智能体?

    作者|Barry Zhang, Anthropic地址|出品|码个蛋(ID:codeegg)整理|陈宇明最近看到了 Anthropic 那篇著名的《Building effective agents》作者之一 Barry Zhang 在 2

    43分钟前
    00
  • 非nvidia卡torchvision报错修复: operator torchvision::nms does not exist

    在Ascend 910b上安装vllm, 会自动把torchaudio和torchvision安装上去.安装前代码语言:shell复制pip list | grep torchtorch

    41分钟前
    00
  • 电脑开机会默认一件GHOST

    关于电脑开机会自己重装系统 前段时间电脑一开机就遇到会自己ghost的问题&#xff0c;而且一直再重复同样的操作&#xff0c;我点击restart的时候到开启页面又会自动ghost&#xff0c;而且此页面停留

    38分钟前
    00
  • 最简 Odoo 部署方法:Websoft9 企业应用托管平台

    传统方式部署 Odoo 通常依赖 Docker 技术,主要分为以下步骤:1 . 安装 Docker需在服务器上安装 Docker 引擎,涉及操作系统兼容性检查、依赖包安装、镜像源配置等操作。代码语言:bash复制 # 以 Ubu

    32分钟前
    00
  • 开源在线考试系统

    看到调问已经开始扩展在线考试的场景,试了一下,发现在线考试的基本能力都已经支持了。主要是考试中的各种计分功能,包括对每道题的选项设置分值计算、考试时间限制等,和官方了解了一下,考试中的其他各项能力也在逐步完善,有需求可以随时

    27分钟前
    00
  • 如何快速判断 Flutter 库是否需要适配鸿蒙?纯 Dart 库无需适配!

    在鸿蒙开发中,选择合适的 Flutter 库至关重要。纯 Dart 库因其跨平台特性,无需适配即可直接使用。但对于新手来说,如何判断一个库是否为纯 Dart 库呢?本文将为你提供清晰的判断方法和实用技巧。一、检查 pubspec.yaml

    22分钟前
    00
  • module &#x27;torch.

    踩坑Ascend, 安装 pytorch 2.5.1 和 pytorch_npu 2.5.1, import torch 报错.执行 python -c "import torch;import torch_npu;"时

    11分钟前
    10
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    4分钟前
    00
  • Windows Server20192022 Evaluation评估版未激活导致关机问题

    摘要&#xff1a;在安装Windows Server 20192022后&#xff0c;会出现系统版本为 Evaluation 评估版情况&#xff0c;如提示Windows许可证已到期&#xff0c;就

    3分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信