React Node.js MySQL Uncaught ReferenceError: process is undefined
P粉471207302
2023-08-29 22:19:27
<p>I'm trying to implement a search page in my website. The user should be able to enter a keyword and the website should return rows from the product table containing that keyword from the database. </p>
<p>I get this error when I try to establish a connection to the database</p>
<pre class="brush:php;toolbar:false;">_stream_writable.js:57 Uncaught ReferenceError: process is not defined
at ./node_modules/mysql/node_modules/readable-stream/lib/_stream_writable.js (_stream_writable.js:57:1)
at options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at fn (hot module replacement:62:1)
at ./node_modules/mysql/node_modules/readable-stream/readable-browser.js (readable-browser.js:4:1)
at options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at fn (hot module replacement:62:1)
at ./node_modules/mysql/lib/protocol/sequences/Query.js (Query.js:7:1)
at options.factory (react refresh:6:1)</pre>
<p>This is the Search.jsx file.我创造</p>
<pre class="lang-js prettyprint-override"><code>import {Component} from "react";
import ItemDisplay from "./itemDisplay";
import item from "./item";
import { test } from "./DB_functions";
class Search extends Component{
constructor() {
super();
this.state = {
items: []
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
console.log(this.state.value);
alert('A name was submitted: ' this.state.value);// instead of this we send this.state.val to back
var input = this.state.value.toLowerCase()
//search in aws products
var mysql = require('mysql');
alert("SQL required");
var sql = "SELECT * FROM products WHERE productName = ?"
var con = mysql.createConnection({
host: "[Removed]",
user: "[Removed]",
password: "[Removed]",
port: '3306',
database: "hs_db"
});
alert("Connection made");
con.query(sql, input, function(err, result)
{
alert("Query Sent");
if (err)
throw err;
else
var usersRows = JSON.parse(JSON.stringify(result));
for (let i = 0; i<usersRows.length; i ){
if(usersRows[i]['name'].includes(input));{
console.log(usersRows[i]['name'])
this.state.items.push(usersRows[i]['name']);
}
}
console.log(usersRows);
})
con.end();
var items = this.state.items
// // this was demo search
// const items = [];
// for (let i = 0; i<this.state.orgitems.length; i ){
// if (this.state.orgitems[i].name.toLowerCase().includes(this.state.value.toLowerCase())){
// console.log(this.state.orgitems[i].name)
// items.push(this.state.orgitems[i]);
// }
// }
this.setState({items});
// end demo
event.preventDefault(); // recieve data, and then pass in data
}render() {
return(
<div class='m-2'>
<div className="input-group">
<input type="search" className="form-control rounded" placeholder="Search" aria-label="Search"
aria-describedby="search-addon" onChange={this.handleChange}/>
<button onClick={this.handleSubmit} type="button" className="btn btn-outline-dark">search</button>
</div>
<ItemDisplay items={this.state.items}></ItemDisplay>
</div>
);
}
}
export default Search;
</code></pre>
<p>I know that when I try to create a connection to the database on this line it throws an error</p>
<pre class="lang-js prettyprint-override"><code>var con = mysql.createConnection({
host: "[Removed]",
user: "[Removed]",
password: "[Removed]",
port: '3306',
database: "hs_db"
});
</code></pre>
<p>But I know the information is correct. I am able to access the database on different clients without any problem. The error is also very obscure, making it difficult to debug</p>
Never let users access your database on the front end. In order to accomplish what you want to do, you need to create a backend server and communicate with it using HTTP requests.
You will find a tutorial on how to do this here.