1

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?

5
  • Are your react components rendered on server side? If no, most likely your library is trying to perform task on client side Commented Sep 29, 2020 at 1:55
  • @DipenShah I'm currently developing using the npm development server. Wouldn't that occur on the "server" side? Commented Sep 29, 2020 at 1:56
  • That is server sure but that doesn't mean you are doing server side rendering. In basic term, server side rendering enabled app just sends plain html, and CSS to client browser while doing all heavy lifting on server side Commented Sep 29, 2020 at 1:59
  • If you are seeing this function call is being made from browser, this will not work. Net library is available only in node environment. Commented Sep 29, 2020 at 2:00
  • Answer here explains it much better than I could in the comments. stackoverflow.com/questions/38005968/… Commented Sep 29, 2020 at 2:02

1 Answer 1

3
+50

React runs in the browser, so as soon as you include database.js in your React app, it's running in the browser with the rest of React, so of course you'll get the error. Making this work gives two options:

  1. expose an API endpoint from Node for React to call, and that API endpoint calls database.js, or,

  2. investigate server-side rendering for React, where some of your React app -- particular the more static parts of it like your main menu -- are created on the Node side and only the final HTML is sent to the browser. (This is a large topic all by itself though.)

By your example which takes a date parameter for the SQL, I'm guessing #2 isn't an option.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.