Search code examples
javascriptmeanjs

Modifying find() to include parameter


Ive created a comments module for each article and i would like to send the Article._id parameter through the find function so it just returns the comments related to that thread. The comments module has an articleID as part of its collection as a way of identifying the relationship.

Im familiar with Java but Mean.js is new to me and i cant figure out why the variable wont go through.

comments.server.controller.js

exports.list = function(req, res, id) {
    Comment.find()
        .sort('-created')
        .where('articleID', id)
        .populate('userName', 'details','created','user')
        .exec(function(err, comments) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.jsonp(comments);
        }
    });
};

view.article.client.view.js

 <section data-ng-controller="CommentsController" data-ng-init="find($scope.deal._id)">

I assume that is all you need to see from the view.

If i remove the where clause it returns them all fine but obviously the $scope.deal._id isnt going through as a String like i assumed it was going to.

How do i properly send through a String to the function?


Solution

  • Mongoose and consequently Mongodb, has the following find method signature:

    Model.find(query, fields, options, callback)

    So in the query parameter you can put objects in order to match with the collection. For example Comment.find({articleID: id_variable})

    More examples