Tuesday, 29 September 2020

Avoiding Net.createConnection errors in Node

I am currently building an app with React and Node.js

In this app, I need to query a database on my own server with the following function, located in a separate file called "database.js"

const fetchQuery = util.promisify(con.query).bind(con)

// Get all the tracks for a given date from the 
const fetchTracks = async (date) => {
  const rows = await fetchQuery("SELECT * FROM tracks WHERE playlistDate = '"+date+"'");
}

This works perfectly when I run the file with Node from the command line. However, when I attempt to import it into my react app with

import { fetchTracks, addTracks } from '../scripts/database'

I begin to get errors in the database file, specifically Unhandled Rejection (TypeError): Net.createConnection is not a function on my fetchQuery call.

From what I've read, this happens when attempting to call the function from the browser, as that would pose a security risk. However, as I understand it, all node operations are performed on the server side, right? Why would I be getting this flag when the database is supposedly queried before the page is served? What do I need to do amend this?



from Avoiding Net.createConnection errors in Node

No comments:

Post a Comment