Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. const leftSidebarReducer = (state = initialState, action ) => {
  2. async function categoryQuery(json)
  3. await fetch('/category_query', { //post request in Server.js
  4. method: 'POST',
  5. headers: {
  6. "Content-Type": "application/json",
  7. },
  8. body: json //currently returns the user inputs
  9. })
  10. .then(function(response){
  11. return response.json(); //**Data I want in store here**
  12. })
  13. .catch(err => console.log(err));
  14. }
  15.  
  16. //--- Further down in the Reducer: ---//
  17.  
  18. case actionTypes.QUERY_DB:
  19. var newdata = [];
  20. var testStr = JSON.stringify({
  21. cat: state.leftMenu.catDrop,
  22. subCat: state.leftMenu.subCatDrop,
  23. insCat: state.leftMenu.insDrop,
  24. langCat: state.leftMenu.langDrop,
  25. });
  26. categoryQuery(testStr) //Run the async function above with state values
  27. return {
  28. ...state,
  29. data: newdata //Where the data should be updating ***
  30. }
  31.  
  32. app.post('/category_query', (req, res) => { //this is the main category query
  33. console.log(req); //this is an attempt to see if we receive the reqs
  34. db.any(
  35. `SELECT * FROM med_services
  36. WHERE cat_name = $1 AND subCat = $2 AND insurance = $3`
  37. ,['Medical Care', 'clinic', 'blue-cross']
  38. //normally would be [req.body.cat, req.body.subCat, req.body.insCat]
  39. )
  40. .then(data => {
  41. console.log('DATA:', data); // print data;
  42. res.send(data);
  43. console.log('category query sent successfully')
  44. })
  45. .catch(error => {
  46. res.send('Error processing query');
  47. })
  48. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement