0

Assume that: http://localhost:7030/profile/5732fe4c56575e05089469dd

How can i get the information from this id 5732fe4c56575e05089469dd in $http url ?

here's the code :

Nodejs :

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:userId
                });
        }
    });
});  

Angular

$http.get("/profile/api/data.json").then(function (response) {
        console.log(response.data);
    });  

something like this : $http.get("url + id + data.json");

How can i access this id from server to angular

Thanks

1 Answer 1

1

Nodejs

router.get('/profile/:id/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:userId
                    });
            }
        });
    });

Angular JS

$http.get("http://localhost:7030/profile/" +id + "/data.json").then(function (response) {
        console.log(response.data);
    }); 
Sign up to request clarification or add additional context in comments.

9 Comments

I got : Error id is not a function
id should have been instatiated before the http is being called. like so var id = "5732fe4c56575e05089469dd";
You have to be getting your UserID from somewhere.. where do you get the userID from
i've passed it from node : res.json({ doc: doc, userID:userId });
You don't pass it like that. You are making a request from your angular code. Not the other way around.. Its the front-end that determines what comes in from the backend
|

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.