Angularjs 从mongoose模式中获取跟随者
基本上我有一个Angularjs 从mongoose模式中获取跟随者,angularjs,node.js,mongodb,express,Angularjs,Node.js,Mongodb,Express,基本上我有一个用户模式。在模式中,我有以下项目,其中包含所有用户的id。用户在数组中跟随 followers: [{ type: String, required: false, unique: false }], 我的问题是:如何仅从追随者数组中获取所有结果,以便对他们运行ng repeat? 这就是我尝试过的 获取追随者控制器: (function(){ angular.module('Scrimbox') .controller('GetFol
用户模式
。在模式中,我有以下项目,其中包含所有用户的id
。用户
在数组中跟随
followers: [{
type: String,
required: false,
unique: false
}],
我的问题是:如何仅从追随者数组中获取所有结果,以便对他们运行ng repeat?
这就是我尝试过的
获取追随者控制器:
(function(){
angular.module('Scrimbox')
.controller('GetFollowingController'
, ['$scope', '$http', 'User', '$routeParams', '$location'
, function( $scope, $http, User, $routeParams, $location){
$scope.following = function(){
//Empty object to send to server
var request = {};
//get follower id
var FollowerId = $routeParams.id;
//bundle into the object to send to server
var request = {
follower: FollowerId
};
//send to server
$http.post('api/social/getfollowing', request)
.success(function(response){
console.log(response);
$scope.following = response.following;
}).error(function(error){
console.log(error);
});
};
$scope.following();
}]);
}());
在服务器上获取以下信息:
var mongoose = require('mongoose');
var User = require('../../models/UserModel');
module.exports.getfollowing = function(req, res){
var thefollower = req.body.follower;
User.findOne({ _id: thefollower }).populate('_following').exec(function (err, user) {
if (err) {
console.log(err);
res.json(err);
} else {
console.log(user);
res.jsonp(user);
}
})
};
调用时使用:
app.post('/api/social/getfollowing', getfollowingController.getfollowing);
这是我从服务器得到的响应:
但是如何仅从以下数组获取所有id
那么,我如何使用它们进行
ng repeat
?好的,我确实收到了这封信,感谢您的回复
我必须为用户查询mongoDB,然后为followee的ID创建一个变量,然后在所有ID上运行forEach循环
var mongoose = require('mongoose'),
User = require('../../models/UserModel'),
async = require('async');
module.exports.getfollowing = function(req, res){
//grab the Users ID from the body
var thefollower = req.body.follower;
//create empty array that i want to populate with the followee's ID and Avatar url
var obj = [];
//query mongo for the user
User.findOne({ _id: thefollower }, function (err, user) {
if (err) {
console.log(err);
res.json(err);
} else {
//grab the following element of the users mongo schema -- should return all the followee's ID's -- tested works
var following = user.following;
//iritate throught all the followee's
async.forEach(following, function(item, callback) {
//current followee
var user = item;
//query mongo for the followee
User.findOne({_id: user}, function(err, followee, callback){
//get followee's ID and Avatar url
var id = followee._id;
var avatar = followee.avatar;
//add the followee's ID and Avatar url to the obj array
obj.push({
id: id,
avatar: avatar
});
});
//see if this worked - returns empty
console.log(obj);
callback();
}, function(err) {
//see if this worked - returns empty
console.log(obj);
//respond to the client - returns empty
res.json(obj);
});
}
});
};
好的,我收到了,谢谢你的回复 我必须为用户查询mongoDB,然后为followee的ID创建一个变量,然后在所有ID上运行forEach循环
var mongoose = require('mongoose'),
User = require('../../models/UserModel'),
async = require('async');
module.exports.getfollowing = function(req, res){
//grab the Users ID from the body
var thefollower = req.body.follower;
//create empty array that i want to populate with the followee's ID and Avatar url
var obj = [];
//query mongo for the user
User.findOne({ _id: thefollower }, function (err, user) {
if (err) {
console.log(err);
res.json(err);
} else {
//grab the following element of the users mongo schema -- should return all the followee's ID's -- tested works
var following = user.following;
//iritate throught all the followee's
async.forEach(following, function(item, callback) {
//current followee
var user = item;
//query mongo for the followee
User.findOne({_id: user}, function(err, followee, callback){
//get followee's ID and Avatar url
var id = followee._id;
var avatar = followee.avatar;
//add the followee's ID and Avatar url to the obj array
obj.push({
id: id,
avatar: avatar
});
});
//see if this worked - returns empty
console.log(obj);
callback();
}, function(err) {
//see if this worked - returns empty
console.log(obj);
//respond to the client - returns empty
res.json(obj);
});
}
});
};
您如何拥有
追随者
,以及如何填充\u追随者
?尝试使用真实的引用:Nathan,你只需要追随者
数组中的ID,对吗?是的,我只需要追随者数组中的ID。你如何拥有追随者
,以及如何填充\u following
?尝试使用真实的引用:Nathan,你只需要追随者
数组中的ID,对吗?是的,我只需要追随者数组中的ID。