1

I cannot send a variable from a js application to node.js server. Here is my code:

//client side

$.get('http://smart-shopper.ro/messages?from=lastGeneralTimeStamp', datas => {
      console.log("data este " + datas)
})
//and server side:

app.get('/messages/:from', (req, res) => {
    let lastGeneralTimeStamp = req.parms.from;
     var sql = `select * from chat where data > '${lastGeneralTimeStamp}' order by id ASC `;
    con.query(sql, (err, result) => {
        if (err) throw err;
       // res.send(result);
       res.send(result);
        console.log(result)
    })
})
Can anyone help me?

Thanks

1
  • Use req.query as indicated in the answer below. Notwithstanding that, you've misspelled req.params (you wrote req.parms. Commented Jul 18, 2018 at 13:55

4 Answers 4

5

You are doing GET to: http://smart-shopper.ro/messages?from=lastGeneralTimeStamp

which is query param with the name from. To access query params you cant use

let lastGeneralTimeStamp = req.parms.from;

but rather

req.query.<name_of_query_param> 

in your case its:

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

1 Comment

In addition, req.params was misspelled (it was req.parms).
1

You are passing data as querystring, but your API on server side is listening data as param.

Change the request in client side:

//client side

$.get('http://smart-shopper.ro/messages/lastGeneralTimeStamp', datas => {
      console.log("data este " + datas)
})
//and server side:

app.get('/messages/:from', (req, res) => {
    let lastGeneralTimeStamp = req.params.from;
     var sql = `select * from chat where data > '${lastGeneralTimeStamp}' order by id ASC `;
    con.query(sql, (err, result) => {
        if (err) throw err;
       // res.send(result);
       res.send(result);
        console.log(result)
    })
})

Or server side API:

//client side

$.get('http://smart-shopper.ro/messages?from=lastGeneralTimeStamp', datas => {
      console.log("data este " + datas)
})
//and server side:

app.get('/messages', (req, res) => {
    let lastGeneralTimeStamp = req.query.from;
     var sql = `select * from chat where data > '${lastGeneralTimeStamp}' order by id ASC `;
    con.query(sql, (err, result) => {
        if (err) throw err;
       // res.send(result);
       res.send(result);
        console.log(result)
    })
})

Comments

1

Try using req.query instead of req.params.

According to this documentation that's how you access the query string.

Comments

0

Possible typo: req.params.from not req.parms.from

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.