Angularjs MEAN.js:获取存储在MongoDB中的用户数
我只是尝试使用MEAN.js向MongoDB提交一个查询。我想获取存储在Mongo中的用户数。(已注册的所有用户的计数) 我正在处理一个由学生编写的MEAN.js网站,它由数千个.js文件组成。我已确定两个文件可能相关: app/controllers/users/users.authentication.server.controller.js 它的函数有Angularjs MEAN.js:获取存储在MongoDB中的用户数,angularjs,node.js,mongodb,express,meanjs,Angularjs,Node.js,Mongodb,Express,Meanjs,我只是尝试使用MEAN.js向MongoDB提交一个查询。我想获取存储在Mongo中的用户数。(已注册的所有用户的计数) 我正在处理一个由学生编写的MEAN.js网站,它由数千个.js文件组成。我已确定两个文件可能相关: app/controllers/users/users.authentication.server.controller.js 它的函数有exports.signup=function(req,res){…}和exports.signin=function(req,res,ne
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
});
}
问题是,假设这个函数甚至起作用。bby工作了,我的意思是返回MongoDB中“用户”记录的计数,不清楚如何调用它。理想情况下,我想称之为前端表面的50个文件
还有另一个文件public/modules/users/controllers/authentication.client.controller.js
这个文件实际上是在前端迭代的,可以在firefox或chrome中调试
它有$scope.signup=function(){…}
和$scope.signin=function(){…}
函数
我希望根据MongoDB中的用户记录数,阻止显示登录页面,或显示另一条消息
当前的问题是,我无法获取authentication.client.controller.js中的计数,因为它不知道“User”是什么。另一个问题是,我不知道如何调用在另一个文件中创建的函数
exports.userCount
,该函数来自前端或authentication.client.controller.js。请不要将服务器控制器与角度控制器混淆
在服务器控制器中,应该有一个返回计数的函数,如您所说的exports.userCount=function(req,res){}代码>。但是,如果要调用该函数(API样式),则必须在用户路由文件中定义路由:
app.route('/my-route-that-retrieves-user-count').get(user.userCount);`
对检索用户计数的路由/my route发出GET请求后,将调用userCount
函数
在angularjs端,您可以在身份验证
控制器中发出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.
});