0

The developer for a project I'm working on left with only partially completing the front-end and back-end and now I'm trying to connect the Node/Express back-end to the React/Redux front-end in time for the project deadline.

The app has a series of drop-downs with a category value (i.e. medical care, counselling, etc.), and once they're all selected and submitted, a Postgres database is queried with said inputs to return all relevant results meeting the users criteria in a series of cards to the user.

The main issue left (which I've identified using Redux dev tools), is that although the query goes through successfully, the action does not update the 'data' property of the state (which should hold an array of services queried).

I've included the code for the reducer that updates the state, as well as the relevant request in the back-end server.js file.

Reducer.js

const leftSidebarReducer = (state = initialState, action ) => {
    async function categoryQuery(json)
        await fetch('/category_query', { //post request in Server.js
            method: 'POST',
            headers: {
                "Content-Type": "application/json",
            },
            body: json //currently returns the user inputs
        })
        .then(function(response){
            return response.json(); //**Data I want in store here**
        })
        .catch(err => console.log(err));
    }

//--- Further down in the Reducer:  ---//

case actionTypes.QUERY_DB:
   var newdata = [];
   var testStr = JSON.stringify({
      cat: state.leftMenu.catDrop,
      subCat: state.leftMenu.subCatDrop,
      insCat: state.leftMenu.insDrop,
      langCat: state.leftMenu.langDrop,
    }); 
    categoryQuery(testStr) //Run the async function above with state values
    return {
       ...state,
       data: newdata //Where the data should be updating ***
    }

Server.js

app.post('/category_query', (req, res) => { //this is the main category query
  console.log(req); //this is an attempt to see if we receive the reqs
  db.any(
  `SELECT * FROM med_services
      WHERE cat_name = $1 AND subCat = $2 AND insurance = $3`
      ,['Medical Care', 'clinic', 'blue-cross']
       //normally would be [req.body.cat, req.body.subCat, req.body.insCat]
   )
    .then(data => {
        console.log('DATA:', data); // print data;
        res.send(data);
        console.log('category query sent successfully')
    })
    .catch(error => {
        res.send('Error processing query');
    })
});

The main issue here is that where the reducer sets data: newData, I'm instead receiving an empty array (i.e. data: []) instead of an array of values from the database.

Another issue is actually passing in the request body into pg-promise (for which I'm only using typed in string values to test at the moment). WHERE cat_name = $1 AND subCat = $2 AND insurance = $3`,['Medical Care', 'clinic', 'blue-cross'] When trying [req.body.cat, etc.] however, I cannot read the property of body from undefined.

James Z
  • 12,209
  • 10
  • 24
  • 44
noe.s
  • 13
  • 1
  • 4

1 Answers1

1

The main issue here is that where the reducer sets data: newData, I'm instead receiving an empty array (i.e. data: []) instead of an array of values from the database.

You should log the actual query being executed, to see what is being executed and why no data is returned. For that, either use initialization option query, or go right for pg-monitor.

Another issue is actually passing in the request body into pg-promise (for which I'm only using typed in string values to test at the moment). WHERE cat_name = $1 AND subCat = $2 AND insurance = $3`,['Medical Care', 'clinic', 'blue-cross']

A better use of pg-promise query formatting is with Named Parameters:

db.any(
  `SELECT * FROM med_services
      WHERE cat_name = $/cat/ AND subCat = $/subCat/ AND insurance = $/insCat/`, req.body)
    .then(data => {...}

When trying [req.body.cat, etc.] however, I cannot read the property of body from undefined.

That tells you that your req is undefined, somehow, and trying to access property body of the undefined throws that kind of error.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • Hi, thank you for the reply! I've installed pg-monitor, though it just shows the query I've written (which actually works and logs the data I want to the server console). I want this data to be able to be passed onto the reducer however, which is not happening and I don't know how to log what's going on there beyond the Redux dev tools which show that the data is set to []. The main issue I think now is that I cannot read the request body in the server in pg-promise... – noe.s Apr 30 '19 at 16:09
  • Seems that you are losing your request data somewhere. This issue is outside of the pg-promise, which is the only side that I could help with. I'm not familiar with the redux. – vitaly-t Apr 30 '19 at 16:26