React Node.js MySQL Uncaught ReferenceError:进程未定义
P粉471207302
P粉471207302 2023-08-29 22:19:27
[MySQL讨论组]
<p>我正在尝试在我的网站中实现搜索页面。用户应该能够输入关键字,并且网站应该从数据库返回产品表中包含该关键字的行。</p> <p>当我尝试建立与数据库的连接时收到此错误</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>这是 Search.jsx 文件。我创造</p> <pre class="lang-js prettyprint-override"><code>import {Component} from &quot;react&quot;; import ItemDisplay from &quot;./itemDisplay&quot;; import item from &quot;./item&quot;; import { test } from &quot;./DB_functions&quot;; 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(&quot;SQL required&quot;); var sql = &quot;SELECT * FROM products WHERE productName = ?&quot; var con = mysql.createConnection({ host: &quot;[Removed]&quot;, user: &quot;[Removed]&quot;, password: &quot;[Removed]&quot;, port: '3306', database: &quot;hs_db&quot; }); alert(&quot;Connection made&quot;); con.query(sql, input, function(err, result) { alert(&quot;Query Sent&quot;); if (err) throw err; else var usersRows = JSON.parse(JSON.stringify(result)); for (let i = 0; i&lt;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&lt;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( &lt;div class='m-2'&gt; &lt;div className=&quot;input-group&quot;&gt; &lt;input type=&quot;search&quot; className=&quot;form-control rounded&quot; placeholder=&quot;Search&quot; aria-label=&quot;Search&quot; aria-describedby=&quot;search-addon&quot; onChange={this.handleChange}/&gt; &lt;button onClick={this.handleSubmit} type=&quot;button&quot; className=&quot;btn btn-outline-dark&quot;&gt;search&lt;/button&gt; &lt;/div&gt; &lt;ItemDisplay items={this.state.items}&gt;&lt;/ItemDisplay&gt; &lt;/div&gt; ); } } export default Search; </code></pre> <p>我知道当我尝试在这一行创建与数据库的连接时会引发错误</p> <pre class="lang-js prettyprint-override"><code>var con = mysql.createConnection({ host: &quot;[Removed]&quot;, user: &quot;[Removed]&quot;, password: &quot;[Removed]&quot;, port: '3306', database: &quot;hs_db&quot; }); </code></pre> <p>但我知道该信息是正确的。我能够在不同的客户端上毫无问题地访问数据库。该错误也非常模糊,因此很难调试</p>
P粉471207302
P粉471207302

全部回复(1)
P粉011684326

永远不要让用户在前端访问您的数据库。为了完成您想做的事情,您需要创建一个后端服务器并使用 HTTP 请求与其通信。

您将找到有关如何执行此操作的教程 此处

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号