MEAN.js:获取存储在MongoDB中的用户数

我只是试图使用MEAN.js提交一个查询到MongoDB。 我想获得Mongo中存储的用户数量。 (所有已经注册的用户的数量)。

我正在处理一个由学生编写的由几千个.js文件组成的MEAN.js网站。 我已经确定了两个文件可能相关:

应用程序/控制器/用户/ users.authentication.server.controller.js

它的function像exports.signup = function(req, res) {...}exports.signin = function(req, res, next) {...}

我补充说:

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

问题是,假设function甚至工作。 和bby工作我的意思是返回MongoDB中的'用户'logging的计数,不清楚如何调用它。 理想情况下,我想把它称为50个文件在前端的表面。

还有另一个文件public / modules / users / controllers / authentication.client.controller.js

这个文件实际上是在前端迭代的,可以用firefox或者chrome来debugging。

它有$scope.signup = function() {...}$scope.signin = function() {...}函数。

我想阻止显示login页面,或者显示一个备用信息,具体取决于MongoDB中用户logging的数量。

目前的问题是我无法获得authentication.client.controller.js中的计数,因为它不知道“User”是什么。 另一个问题是,我不知道如何调用我在另一个文件中创build的函数exports.userCount ,从前端或authentication.client.controller.js。

不要把服务器控制器误认为angular度控制器。

在服务器控制器中,你应该有一个返回计数的函数,像你所说的: exports.userCount = function(req, res) {}; 。 但是,如果您想调用该函数(API风格),则必须在用户path文件中定义路由:

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

一旦对路由/my-route-that-retrieves-user-count发出GET请求,你的userCount函数将被调用。

在angularjs方面,你可以在你的Authentication控制器中使用GET请求(从angularjs文档改编):

 $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. });