0

I want to show current user information from nodejs to angular(view).

The problem is that i don't know how to pass and how to get User Id in node and angular .

Code :

Node

router.get('/:id/api/data.json', function(req, res, next) {
    console.log(req.params.id);
    var userId = req.params.id;
    User.findById({_id:userId}, function (err, doc) {
        if (err) throw err
        if (doc){
            res.json({
                doc: doc,
                userID:req.params.id
                });
        }
    });
});

Angular :

 app.controller('profileCtrl', function($scope, $http) {
    $http.get("don't know how to get id from node").then(function (response) {
        console.log(response.data);
    });
});
6
  • What is output in your browser when you console.log(response.data)? Also, have you tried user res.jsonp() instead of res.json()? The rest looks good to me. Commented Jun 1, 2016 at 10:49
  • the problem is that i don't know what should i put in url : $http.get("don't know how to get id from node") Commented Jun 1, 2016 at 10:50
  • Ah, sorry - I skimmed over that part too quickly. You just put in the url that Node is watching: $http.get('/' + id + '/api/data.json').then... Since Node is watching '/:id/api/data.json' this is the url you have to make the $http.get request to. Commented Jun 1, 2016 at 10:57
  • i got this error : id is not defined Commented Jun 1, 2016 at 15:33
  • From Angular/your browser? You would need to pass in an ID as a variable. I'll explain in an answer below so I can be a bit more thorough. Commented Jun 1, 2016 at 15:45

1 Answer 1

1

Your Node.js router is listening to the url /:id/api/data.json. The :id part of that means Node.js is expecting a paramater there, which will get picked up by req.params.id in your Node.js file.

This means that you actually have to be passing in an id value as a part of the url. So your url would look something like /userid12345/api/data.json.

In your Angular file, that's the url you're going to be making the get request to. Which means you need to know the user's ID in your Angular file so you can get that specific url, e.g.:

var userId = 'userid12345';

$http.get('/' + userId + '/api/data.json').then(function(response) {
    console.log(response);
});

Once you pass userId in as a part of the url, then Node can grab it using req.params.id and you can make your db call.

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

8 Comments

i did this and i got error too : var id = userId i got id is not defined
id and userId are just variables that I made up. Do you have an actual user ID in your database? If so, use that, then set your variable in your Angular file to the user ID in your database, i.e. var userId = 'whatever user id is in your database';
_id is in my mongo db , var userId = _id ; i got that error again
_id is set for every db entry in mongo db. What's the value associated with _id in your mongo db entry? It should look something like 574eef74555erss983290f283.
yes it's something like this 574eef74555erss983290f283, but how can i pass this Id (dynamiclly) to angular controller ?
|

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.