user237462
user237462

Reputation: 421

Mean.js - Using URL as a Mongoose parameter

What im trying to do is use an ArticleID in the URL i.e article/55cf3ea22440c5542c9fa333 as the parameter to filter related comments.

Controller

exports.list = function (req, res) {

Comment.find(

     {articleID: req.params.articleId}

)
    .sort('-created')
    .populate('user', 'displayName')
    .exec(function (err, comments) {

        if (err) {

            return res.status(400).send({

                message: errorHandler.getErrorMessage(err)
            });

        } else {

            res.jsonp(comments);
        }
    });

};

The comments and Articles are on the same page with the comments at the end of the post like on Stackoverflow. The url is http://localhost:3000/#!/articles/55cf3ea22440c5542c9fa333

is there a simple way of utilising this in the controller query?

Route

app.route('/articles/:articleId')
    .get(articles.read)
    .put(users.requiresLogin, articles.hasAuthorization, articles.update)
    .delete(users.requiresLogin, articles.hasAuthorization, articles.delete);

Upvotes: 0

Views: 108

Answers (1)

chalasr
chalasr

Reputation: 13167

If you are using $routeProvider as router, you can access to your param using this :

Your route and param in the url:

  $routeProvider
    .when('/:articleId', {  //The parameter you want get( articleID )
      templateUrl: 'views/article.html',
      controller: 'ArticleCtrl'
  })

Pass $routeParams in your controller and get param using:

  $routeParams.articleID

EDIT :

If you are using $stateProvider, you have to pass $stateParams into your controller, and get params using :

  $stateParams.articleID

I hope this is what you need.

Upvotes: 1

Related Questions