抓住mongoose架构的粉丝

时间:2015-12-01 12:02:56

标签: angularjs node.js mongodb express

基本上我有User schema。在架构中,我有以下项目,其中包含所有用户的id'suser跟在数组中。

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

    }]);
}());
服务器上的

getfollowing

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

这是我从服务器获得的响应: screenshot

但是,我如何才能从以下数组中获取所有ids

然后如何将它们用于ng-repeat

1 个答案:

答案 0 :(得分:0)

好的,我确实感谢你的回复。

我必须为用户查询mongoDB,然后为跟随者的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);  

                });                  
            }   
    });  
};