Para mi aplicación de reacción, tengo un componente de inicio de sesión al que queremos llamar una función de registro que se encuentra en un archivo dbUtils separado, las funciones funcionan cuando el archivo se ejecuta solo, pero cuando se llama en el componente de reacción, obtengo el mensaje de error.
TypeError: Net.createConnection is not a function 71 | this._connectCalled = true; 72 | 73 | // Connect either via a UNIX domain socket or a TCP socket. > 74 | this._socket = (this.config.socketPath) | ^ 75 | ? Net.createConnection(this.config.socketPath) 76 | : Net.createConnection(this.config.port, this.config.host); 77 |Este es el componente SignIn.js en nuestra aplicación de reacción
import * as db from '../utils/dbUtils.js'; const SignInModal = ({ showSignIn, setShowSignIn }) => { // ---- ERROR FURTHER BELOW ---- const modalRef = useRef(); const [showSignUp, setShowSignUp] = useState(false); function submitForm() { if (showSignUp) { db.signUp(); } setShowSignIn(false); values.email = ''; values.username = ''; values.password = ''; values.password2 = ''; } useEffect( () => { document.addEventListener('keydown', keyPress); db.initCon(); //<----------------------- ERROR OCCURS HERE ------------- return () => document.removeEventListener('keydown', keyPress); }, [keyPress] ); return ( <> <div>lots of html</div> </> ); } export default SignInModaleste es el archivo javascript que contiene nuestra lógica de inyección sql dbUtils.js
var mysql = require('mysql'); var connection; var loggedIn = false; var currAccId; export function initCon(){ connection = mysql.createConnection({ host : '---', // <-- taken out for this post user : '---', password : '---' }); connection.connect(); } export function endConn(){ connection.end(); } export function signUp(dispName, email, pw){ connection.query("INSERT INTO typeTestdb.user_table (display_name,user_email,password) VALUES (?,?,?);",[dispName, email, pw], function (error, results) { if (error) { console.log("Email already registered! Try logging in!"); } else { console.log('Account Created'); } }); }