简体   繁体   English

使用 React JS 应用程序在 Mysql 数据库中编辑和保存数据

[英]Edit and Save data in Mysql Database using React JS application

I'm trying to do CRUD operations using Mysql Database and Node Js.我正在尝试使用 Mysql 数据库和节点 Js 进行 CRUD 操作。 In my requirement i need to post data to the data base and view it on the window after the user logged in if the user wants to edit the contents in the data he can edit it using the edit button.在我的要求中,我需要将数据发布到数据库并在用户登录后在 window 上查看,如果用户想要编辑数据中的内容,他可以使用编辑按钮对其进行编辑。 Here I'm able to do all operations except editing the data and send it back to database when the user logged in next time the data need to be the updated data.在这里,我可以执行除编辑数据之外的所有操作,并在用户下次登录时将其发送回数据库,此时数据需要是更新的数据。

Here I'm attaching the code snippet below在这里,我附上下面的代码片段

 import React, { Component } from "react"; class Home extends Component { constructor() { super() this.state = { tableContent: [], task: '' } } logout() { window.location.href = '/login'; } componentDidMount() { fetch('http://localhost:5001/getcontent', { method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, // body: JSON.stringify({task: this.state.task, id: this.state.editableTaskId}), }).then(Response => Response.json()).then(json => this.parseFunction(json)).catch(err => { console.log('error', err); }); { /*.then(res => res.json()).then(data => this.setState({ task: '', editableTaskId: null, tableContent: this.state.tableContent.map(indx => indx.id === data.id? data: indx)})).catch(err => console.log(err))*/ } } parseFunction(result) { console.log('result', result) this.setState({ tableContent: result }); }; handleEdit = id => this.setState({ task: this.state.tableContent.find(indx => indx.id).task }) render() { return ( < div className = "contentbox" > < input type = "button" className = "btn1" id = "Logout" value = "Logout" onClick = { this.logout } /> { this.state.tableContent.map((tableContent, index) => ( < p > < p style = { { float: "left" } } > { tableContent.title } - { tableContent.content } < /p><br></br > < button onclick = { this.handleEdit } > Edit < /button> < /p> )) } < /div> ) } } export default Home;

Here if i use the commented code which i tried to fix the issue no data is displaying on the Home Page.在这里,如果我使用我尝试解决问题的注释代码,则主页上不会显示任何数据。

Here is the code for data base which is in node js.这是节点js中的数据库代码。

In my Table I'm using Id,title & content .在我的表中,我使用Id,title & content

Any help regarding this issue will be more than helpful for me.关于这个问题的任何帮助对我来说都是有帮助的。 Thanks in advance.提前致谢。

// Capitalized Response is a keyword, using lowercase just in case
.then((Response) => Response.json())

// your not passing the id here to handleEdit
<button onclick={this.handleEdit}>Edit</button>
// should be:
// <button onclick={() => this.handleEdit(tableContent.id)}>Edit</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM