SOS问题请: 我想制作一个简单的待办事项应用程序来添加待办事项任务并将其保存在数据库 mysql 中,就像此链接一样: https://www.w3schools.com/howto/howto_js_todolist.asp
但是我想将数据保存在mysql数据库中而不是li html标签中,并在我单击添加(提交按钮)时直接渲染它,就像上面的示例一样! 问题是我找不到如何在 Nodejs 中创建新的 li 元素,或者在连接数据库后如何处理数据行! 任何人都请指点一下!求救
最佳答案
您好,欢迎来到SO!
我不确定我是否理解你想要完成什么,但一般来说,你的数据流应该是这样的:
将数据存储在数据库中:
- 在按钮上添加事件处理程序,该处理程序将使用 JS 获取您输入的任务字符串,将其存储在变量中,并使用 REST API(例如 XMLHttpRequest)将此变量传递到您的服务器(Express 是很好的 Nodejs FW)
- 服务器现在连接到池并将数据发送到特定表,完成后,您将从服务器获得 HTTP 响应
从数据库获取数据:
- 使用相同的按钮事件处理程序 - 在收到 http200 之后 - 您开始使用 JS 创建
- 元素,并将收到的作为响应的字符串以及 http 状态附加到其中。
令人困惑的部分 - 当您想要实现的一切是将新任务附加到表单下方的任务列表时,为什么要使用数据库?
EDIT to explain API data flow further:
前端:
首先,您需要按钮的点击处理程序:
<button type="button" onclick="createNewProject()">Click Me!</button>
它将调用类似的函数:
let createNewProject = () => {
const xhr = new XMLHttpRequest() //REST API: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
const method = "post" //HTTP request method: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
const uri = "/createnewproject" //URI which you use to communicate with backend
//putting data you want to send to server together:
newProject = {}
newProject.project_name = document.getElementById("modal_project_name").value
newProject.project_description = document.getElementById("modal_project_description").value
//send REST request:
xhr.open(method, uri)
xhr.setRequestHeader("Content-Type", "application/json")
xhr.send(JSON.stringify(newProject))
//receive response from server and handle it on frontend:
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
//do whatever your frontend needs to do
}
}
服务器:
// please note that HTTP request type and URI do match with frontend
app.post("/saveproject", (req, res) => {
let savedProject = req.body; //get data from frontend
persistence.saveProject(savedProject, status => {
res.status(status).send(); //send to persistence
});
});
坚持:
这里使用PG。您需要使用数据库服务器和连接的地址创建连接配置文件(基于数据库类型,遵循文档),当然还要导入需要在 Node 端导入的任何内容。
module.exports.createNewProject = (newProject, callback) => {
// queryText: SQL query
// queryValues: your data
const queryText = "INSERT INTO projects (project_name, project_description, project_statuses_id, updated_date) VALUES ($1, $2, (SELECT id FROM project_statuses WHERE project_status = 'active'), $3)"
const queryValues = [
newProject.project_name,
newProject.project_description,
newProject.updated_date
]
pool.connect((err, client, done) => {
if (err) {
console.log("PERSISTENCE: ", err)
done(err)
callback(400)
return console.error("POOL CONNECTION ERROR", err.code)
}
client.query(queryText, queryValues, (err, result) => {
if (err) {
console.log("PERSISTENCE: ", err)
done(err)
callback(400)
return console.error("QUERY ERROR", err.code)
}
done()
callback(200) //return HTTP response back to frontend https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
})
})
关于javascript - 我如何从 mysql 获取数据并使用 nodejs 和 javascript 渲染它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52533656/