1

I'm simply trying to submit a query to MongoDB using MEAN.js. I want to get the number of users stored in Mongo. (the count of all users who have registered).

I'm working with a giant mess of a MEAN.js website, written by students, that is made up of thousands of .js files. I have identified that two files are likely related:

app/controllers/users/users.authentication.server.controller.js

which has functions like exports.signup = function(req, res) {...} and exports.signin = function(req, res, next) {...} in it.

I added:

exports.userCount = function(req, res) {
    // use mongoose to get the count of Users in the database
    User.count(function(err, count) {

        // if there is an error retrieving, send the error. nothing after res.send(err) will execute
        if (err)
            res.send(err)

        res.json(count); // return return the count in JSON format
    });
}

The problem is, assuming that function even worked. and bby worked I mean return the count of 'User' records in MongoDB, it is not clear how to call it. Ideally, I'd like to call it 50 files toward the surface on the front end.

There is another file, public/modules/users/controllers/authentication.client.controller.js

This file is actually iterated through on the front end, and can be debugged in firefox or chrome.

It has $scope.signup = function() {...} and $scope.signin = function() {...} functions.

I want to prevent the signin page from being displayed, or display an alternate message, depending on the number of User records in MongoDB.

The current problem is that I can't get the count in authentication.client.controller.js because it doesn't know what 'User' is. The other issue is that I don't know how to call the function exports.userCount that I created in the other file, from the front-end, or from authentication.client.controller.js.

1 Answer 1

1

Do not mistake server controllers with angular controllers.

In server controllers, you should have a function that returns the count like you said exports.userCount = function(req, res) {};. However if you want to call that function (API style) you have to define a route in your user route file:

app.route('/my-route-that-retrieves-user-count').get(user.userCount);`

Once a GET request is made to the route /my-route-that-retrieves-user-count, your userCount function will be called.

In the angularjs side you can make the GET request like (adapted from angularjs docs) from within your Authentication controller:

$http({
    method: 'GET',
    url: '/my-route-that-retrieves-user-count'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    // response will be the count of users
    // you can assign that value to a scope and
    // act accordingly to its value
}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});
Sign up to request clarification or add additional context in comments.

Comments

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.