0

I have followed next tutorial: http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/ After a few days I got it all working, but updating the userdatabase is for the reader themselves.

I have edited the jade template and added some javascript for a click event on an update link. I could even retrieve the userID from a listed testuser. Tested this with an alert and is ok.

javascript for click event: global.js

function showUser2Edit(event){
    event.preventDefault();

    var userID = $(this).attr('rel');
    // alert (userID);// _id from MongoDB, ok!

    // jQuery AJAX call for JSON
    $.getJSON( '/users/userdata/' + userID, function( data ) {
        // populate edit form
        alert (JSON.stringify(data));
    }
}  

in users.js (server-side node.js)

/*
 * GET userData
 */
router.get('/userdata', function(req, res) {
    var db = req.db;
    var collection = db.get('users');
        collection.find({_id: ?????? },{},function(e,docs){
        res.json(docs);
    });
});

2 problems i really have no idea how to solve them:

Ajax call will be to: users/userdata/_id (_id is a long string, automatically added as key by MongoDB). But how do I add this to this router function?

This _id must be placed in the where part of collection.find(....), but how do i extract this from the url?

Thanks in advance for any help!

The mark for duplication is not correct,that one is about a query parameter: ?color=red, how can I access the variabe color?,mines is a REST system where the id is part of the url itself.

Terradon
  • 883
  • 2
  • 13
  • 33
  • 2
    This an "express" question and the answers are found in the documentation for the ["request"](http://expressjs.com/api.html#req) object. In fact, it's the first example on the page. – Blakes Seven Sep 24 '15 at 13:05
  • Let me google that for you.. :) Also a duplicate. – Andrey Popov Sep 24 '15 at 13:08
  • 1
    possible duplicate of [How to access the GET parameters in Express.js or Node.js?](http://stackoverflow.com/questions/17007997/how-to-access-the-get-parameters-in-express-js-or-node-js) – Andrey Popov Sep 24 '15 at 13:09
  • that one is about a get parameter: ?color=red, how can I access the variabe color? My question is about a REST system, so witout parameters – Terradon Sep 24 '15 at 13:16
  • @Blakes: thanks for that link. – Terradon Sep 24 '15 at 13:44
  • Perhaps now you might understand that this "is" a GET parameter just as was suggested in the duplicate. It's just another way that the framework translates a parameter from the URL as an URL "part" rather than just an argument via `?`. Since it's nicer that way. – Blakes Seven Sep 24 '15 at 13:47
  • 3days ago i thought that the word REST stands for taking a nap:) I have to read a lot more to fully understand its principles. – Terradon Sep 24 '15 at 13:50

2 Answers2

4

You need change

router.get('/userdata'...

To:

router.get('/userdata/:id'...

:id - its params. If you use body-parser in node.js that check req.body.params.

  • 1
    Come on, it's a duplicate. Don't answer such a questions as you seem to be the kind of guy that simply wants some points :) – Andrey Popov Sep 24 '15 at 13:13
  • 1
    Sometimes it's hard to find the "easy" solution. Not everyone has outstanding google skills, sometimes you just get stuck after 50+ examples. Sometimes the duplicate flag is set too fast. I really respect the rules on this Q&A site, but sometimes it is in a real "very strict" modus. Nothing is wrong with giving a simple answer, This one is really simple, but it is also an eyeopener for me! I AM NOT LAZY,SPENT 3 DAYS TO THIS EXAMPLE, in get it running and understanding. Sometimes,people like me, search everywhere, but not always on the right places. This is also a learning proces! – Terradon Sep 24 '15 at 13:42
  • You've never opened the documentation, or you should've seen it already. It's not about googling skills, it's about trying to find it first. – Andrey Popov Sep 24 '15 at 13:44
  • @Terradon Usually the duplicate flag is set because you did not find it yourself and someone is trying to "help" by pointing you towards an answer that already exists. Close votes are not an insult, but meant to guide you or protect you from others submitting bad answers. Which is what happens typically if your question sits around to long. Thank the nice person for helping instead of being so defensive and offended. Better way to view things here. Nothing really wrong with answers on duplicates ( though I would prefer not ), just as long as both are acknowledged. – Blakes Seven Sep 24 '15 at 13:53
  • Thanks for the explanation, I had no offensive intentions. Of course I am happy with all FREE advice I get here. Must be tired because of the last 3 days i spent on this example:). . – Terradon Sep 24 '15 at 14:02
  • Terradon, you can also use req.query too. Just add in $.getJSON( '/users/userdata/?' + userID... – Vladyslav Mysiur Sep 24 '15 at 14:06
  • ???? will be replaced with the value of _id, so it becomes a select query with a where clausule of: _id = userID. (Mongo way of doing this). Misunderstanding your comment. I am trying to build a REST system. (getting data: GET, inserting data:POST, updating data: PUT, deleting data: DELETE) – Terradon Sep 24 '15 at 14:09
0

You change:

$.getJSON( '/users/userdata/' + userID, function( data ) {

To:

 $.getJSON( '/users/userdata/?id=' + userID, function( data ) {

And use in node - req.query.id.