mysql - 如何将 MySQL 数据库中的数据放入 snake case 中,并使用 express 将其放入 ejs 列表中每个项目的值字段中

标签 mysql node.js express ejs

我正在准备一个表单,用于从数据库中获取本地企业的一些数据。我能够从我的 MySQL 数据库中动态获取数据并将其放入下拉列表中,但我无法将选项标签附带的值填充为项目获取的值,因为它有空间。我决定使用 lodash 将数据转换为蛇形大小写,但我无法将其放入值字段中。

App.js 代码

const express = require("express");
const bodyParser = require("body-parser");
const db = require("./util/database");
const _ = require("lodash");

const app = express();

app.set("view engine", "ejs");

app.use(
  bodyParser.urlencoded({
    extended: true
  })
);

app.listen(3000, function() {
  console.log("Server Started at port 3000");
});

app.get("/", function(req, res) {
  db.execute("SELECT customerName FROM customers")
    .then(([rows, fieldData]) => {
      // console.log(_.snakeCase(rows[0].customerName));
      const map = rows.map(name => _.snakeCase(name.customerName));
      res.render("home.ejs", {
        customerName: rows,
        customerNameSnakeCase: map
      });
    })
    .catch(err => {
      console.log(err);
    });
});

app.post("/", function(req, res) {
  const quantity = req.body.quantity;
  const rate = req.body.rate;
  const amount = quantity * rate;

  res.render("another", {
    amountValue: amount
  });
});

home.ejs代码

<%- include("partials/header"); -%>
</head>

<body>

    <form class="form-control" action="/" method="post">
        <input type="date" name="date">
        <label for="salePurchase">Type of Transaction</label>
        <select required>
            <option value="sale">Sale</option>
            <option value="purchase">Purchase</option>
            <option value="other">Others</option>
            <option value="payment">Payment</option>
            <option value="receipt">Receipts</option>
        </select>
        <label for="vehicleNumber">Vehicle Number</label>
        <input type="number" , name="vehicleNumber">
        <label for="partyName">Party Name</label>

        <select>
            <% customerName.forEach((name) => { %>
            <option value=""><%= name.customerName %></option>
            <% }) %>
        </select>

        <select>

        </select>
        <label for="factory">Factory</label>
        <select>
            <option value="Butibori">Butibori</option>
            <option value="Hingna">Hingna</option>
        </select>
        <label for="group">Group</label>
        <select name='group'>
            <option value='finishedProduct'>Finished Products</option>
            <option value='scrapMaterial'>Scrap Material</option>
            <option value='rerollingMaterial'>Rerolling Material</option>
            <option value='coal'>Coal</option>
            <option value='machinery'>Machinery</option>
            <option value='freight'>Freight</option>
        </select>
        <label for="item">Item</label>
        <select name='item'>
            <option value='squareBar'>Square Bar</option>
            <option value='roundBar'>Round Bar</option>
            <option value='flatBar'>Flat Bar</option>
            <option value='angle'>Angle</option>
            <option value='gateChannel'>Gate Channel</option>
        </select>
        <label for="itemSpecifics">Item Specifics</label>
        <select name='sqaureBar'>
            <option value='6mmFixed'>6mm Fixed</option>
            <option value='6mmRandom'>6mm Random</option>
            <option value='8mmFixed'>8mm Fixed</option>
            <option value='8mmRandom'>8mm Random</option>
            <option value='10mmFixed'>10mm Fixed</option>
            <option value='10mmRandom'>10mm Random</option>
            <option value='12mmFixed'>12mm Fixed</option>
            <option value='12mmRandom'>12mm Random</option>
        </select>
        <input type="number" name="quantity" placeholder="Total Quantity">
        <input type="number" name="rate" placeholder="Rate per Ton">
        <input type="number" name="amount" placeholder="Total Amount">
        <input type="text" name="comments" placeholder="Comments">
        <button type="submit">Submit</button>
    </form>

    <%- include("partials/footer"); -%>

现在我没有在值字段中输入任何内容,但我想要在蛇形案例中生成的客户名称。顺便说一下,MySQL 数据库有一个 customers 表,里面有几个名字。

最佳答案

您不能将这两个值都放在您的 map 中吗?

//App.js
const map = rows.map(name => {displayName: name.customerName, snakeCaseName: _.snakeCase(name.customerName)});

然后您可以省略渲染函数的参数之一:

//App.js
res.render("home.ejs", {
customerNames: map
});

并渲染它:

<select>
<% customerNames.forEach((name) => { %>
<option value=<%= name.snakeCaseName %>><%= name.displayName %></option>
<% }) %>
</select>

关于mysql - 如何将 MySQL 数据库中的数据放入 snake case 中,并使用 express 将其放入 ejs 列表中每个项目的值字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56087912/

相关文章:

php - 尝试在 MYSQL 中执行 SELECT 语句但不起作用

javascript - nodejs获取配置文件中的当前url

node.js - 尝试打开 heroku 应用程序时出现应用程序错误消息

node.js - 重定向 Nodejs Express 应用程序内 npm 模块 "request"发出的异步调用

javascript - 如何将所有请求重定向到特定域名?

javascript - Express Nodejs,模块化应用程序

mysql - 同时从两个表中选择?

Mysql,每N行删除几行

mysql - 如何检索添加到数据库表中的行?

debugging - 在子进程上使用 Node.js 命令行调试器?