【问题标题】:how to enter html data into postgres database using nodejs如何使用nodejs将html数据输入postgres数据库
【发布时间】:2022-06-13 20:35:16
【问题描述】:

所以我想使用 node 将表单中输入的数据输入到 postgres 表中。我已经制作了表。我希望更新数据,使用 html 表单输入它们。

代码如下。 server.js 文件

const {Pool} = require('pg');
const express = require ('express');
const app = express();

app.use(express.json());

const pool = new Pool({
user : 'postgres',
host : 'localhost',
database : 'postgres',
password : 'wasay123',
post : 5432,
})

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

app.get("/", (req, res) => res.sendFile(`${__dirname}/web.html`));

app.post("/", function(request, response){ 
  //  pool.query('UPDATE "accounts" SET "balance" = "balance" - $1 WHERE "id" = $2', 
 // [amount,from]);
 //pool.query('UPDATE "accounts" SET "balance" = "balance" + $1 WHERE "id" = $2', 
//[amount, to]);
//console.log(request.body.data);  
response.send(request.body);  
console.log(request.body);   
}); 

app.listen(8084, () => console.log("Web server is listening.. on port 8080"))

start()

async function start() {
  await connect();
}
 async function connect() {
try {
    await pool.connect(); 
}
catch(e) {
    console.error(`Failed to connect ${e}`)
}
}

html代码如下web.html文件

<!DOCTYPE html>
<html>
<head>
 <style>
 ul {
 list-style-type: none;
 margin: 0;
 padding: 0;
 overflow: hidden;
 background-color: #333;
}

li {

浮动:左; }

display: block;
color: white;
text-align: center;
 padding: 14px 16px;
text-decoration: none;

}

li a:hover:not(.active) {
 background-color: #111;
}

 .active {
  background-color: #04AA6D;
 }
  body {
  background-image: url('https://cdn.pixabay.com/photo/2015/04/23/22/00/tree- 
  736885__480.jpg');
  background-repeat: no-repeat;
  background-size: cover;
 }
 div {
 margin-top:100px;
 margin-left:500spx;
 }


  </style>
  </head>
  <body>

  <ul>
  <li><a class="active" href="#home">Home</a></li>
   <li><a href="#news">News</a></li>
   <li><a href="#contact">Contact</a></li>
   <li><a href="#about">About</a></li>
</ul>

<div>
 <form action="/" method="POST">
  <label for="from">From:</label>
 <input type="text" id="from" name="from"><br><br>
<label for="to">To:</label>
 <input type="text" id="to" name="to"><br><br>
 <label for="amountt">Amount</label>
 <input type="number" id="amountt" name="amountt"><br><br>
 <input type="submit" value="Submit">

 </form>
 </div>

 </body>
</html>

如您所见,查询不起作用。

【问题讨论】:

    标签: html node.js postgresql


    【解决方案1】:

    您的app.post("/") 方法中目前没有插入语句。您可以使用 Promise 来创建和处理查询。你的函数看起来像这样..

     app.post("/", async (request, response) => {
      pool.insertQuery = (columnName) => {
        return new Promise((resolve, reject) => {
          pool.query(
            "INSERT INTO tableName (columnName) VALUES (?)",
            [columnName],
            (error, result) => {
              if (error) {
                return reject(error);
              }
              return resolve(result);
            }
          );
        });
      };
    });
    

    【讨论】:

    • 我希望每次提交表单时都会更新表中的数据,因此此处不应使用“更新”语句。插入“?”的插入语句中的值也应该是什么。这就是我面临问题的地方。我希望这些值成为表单输入的参数。每次运行该语句时都需要将值添加到表中。
    猜你喜欢
    • 2020-09-03
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2021-09-30
    • 2021-04-17
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多