0

What I'm trying to accomplish is having an input box called "Order Number" the user can put in an order number and hit submit. When they hit submit, it redirects to another page where fields would get captured based on that order number that was enterered on the previous page for the input box.

What I have figured out so far is:

  • passing a post request to the / route which is the page where the input box is on for the user to put in the order number.
  • With help, I managed to pass the orderNum in the url on the next page

What I'm stuck on is trying to figure out how to fetch the orderNum that I passed in the URL to the new route on the server. Once I can fetch it I'll know how to set it to state and populate the boxes with the orderNum

Hope that was clear enough

My Attempt:

app.post('/', function(req, res){
    request.query("select [TableId] FROM [SACH_QA].[Orders].[Order] where [TableId] ='" + req.body.orderNum + "'", function(error, result){
        if(error){
            throw error;
        } else {
            //console.log(result);
            res.redirect('/relotoForm');
        }
    });
});

app.post('/reloToForm', function(err, res){
    if(err){
        throw err;
    } else {
        // How can I have access to req.body.orderNum from / on this request?
    }
});

View:

onSubmit(e){
    if(this.state.orderNum === '') {
        this.setState({
            errorMsg: 'Please enter an order number.'
        });
        e.preventDefault();
    } else {
        this.setState({
            errorMsg: ''
        });
        
        console.log('passed');
        // Submit the form

        const reloData = {
            orderNum: this.state.orderNum
        }

        fetch('/', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify(reloData)
        }).catch(function(err){
            console.log(err);
        })
            
    }
    //e.preventDefault();
}
Curious13
  • 291
  • 2
  • 17

1 Answers1

0

I think you can make a route like this:

app.post('/', function(req, res){
        request.query("select [TableId] FROM [SACH_QA].[Orders].[Order] where [TableId] ='" + req.body.orderNum + "'", function(error, result){
            if(error){
                throw error;
            } else {
                //console.log(result);
                res.redirect('/relotoForm' + '?orderNum=' +req.body.orderNum);
            }
        });
    });

and here you can access it as a orderNum:

app.post('/reloToForm', function(err, res, req){
            if(err){
                throw err;
            } else {
                console.log(req.query.orderNum)
            }
        });

I hope it will work, didnt tryed by myself

peter
  • 305
  • 1
  • 2
  • 12
  • Let me try it real quick thank you for the suggestion! – Curious13 Feb 15 '18 at 19:35
  • Take care about the route names, at the first case you used /relotoForm', and at the second time: '/reloToForm' – peter Feb 15 '18 at 20:04
  • I was able to append the order number in the url. However, I'm having difficulty fetching the orderNum over that new request which is /relotoForm says it's undefined when I try to fetch it. In addition, I think I need to change my /relotoForm route to a get in order to fetch that route with the orderNum in the url correct? – Curious13 Feb 15 '18 at 20:28
  • I think you need to preserve the HTTP verb (POST) when you redirect, using something like this: `res.redirect(307, '/relotoForm' + '?orderNum=' +req.body.orderNum); // – Andy Hoffman Feb 15 '18 at 20:36
  • are you trying from browser? – peter Feb 15 '18 at 20:49
  • If you don t really need the post method, than you should change it to get in the 2.nd case – peter Feb 15 '18 at 20:50
  • What you get if you log out the req at the second case? – peter Feb 15 '18 at 21:04
  • In the / route, if I log the result from the query it gives the result that I inputted from my form input box which I wanted. However, in my new route where I tried to pass it to, on my browser when I try to fetch it, it gives me [object object] – Curious13 Feb 15 '18 at 21:07
  • read the accepted answer, and let me know if you really needs the POST method in the 2.nd case: https://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get – peter Feb 15 '18 at 21:13
  • I need post because I'm submitting data to another page through a form. However, on the relotoForm route even though I passed the orderNum in the url, when I try to hit the submit button, and I console log the orderNum on that route, its giving me [object Object] – Curious13 Feb 15 '18 at 21:32
  • try to log out the req.query – peter Feb 15 '18 at 21:42
  • Sorry mistake when I log out req.query it returns in the console, {} – Curious13 Feb 15 '18 at 22:05