0

I want to display friends of Authenticated user in angularjs page.

// Find a list of Friends
    $scope.find = function() {
        $scope.friends = Authentication.user.friends;
        $scope.firstFriendName = Authentication.user.friends;
    };

I'm using mongoose with nodejs.(MeanJS)

How can I populate friends of current user in meanjs?

Thanks.

Emre Alparslan
  • 1,022
  • 1
  • 18
  • 30

2 Answers2

0

Either extend the user object or add a custom model for example Person , that consists of both user reference, and list of friends:

/**
 * Person Schema
 */
var PersonSchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Please fill Person name',
        trim: true
    },
    desc:{
      type: String,
      default: '',
      trim: true
    },
    friends:[{
      rate: Number,
      date: Date
    }],
    ,
    created: {
        type: Date,
        default: Date.now
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});

mongoose.model('Person', PersonSchema);

Then write the corresponding controller and views. Or just use meanJS generators:

yo meanjs:crud-module Persons

Then add the appropriate changes to the module. By the way, there is a package for something similar that seems to patch the User schema: moongose-friends

Community
  • 1
  • 1
David Karlsson
  • 9,396
  • 9
  • 58
  • 103
0
var PersonSchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Please fill Person name',
        trim: true
    },
    desc:{
      type: String,
      default: '',
      trim: true
    },
    friends:[{
      type: Schema.ObjectId,
      ref: 'User'
    }],
    ,
    created: {
        type: Date,
        default: Date.now
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});

Then you would use the populate() method built into Mongoose which MEANJS uses. You can actually see how this works if you use the generators to build a CRUD module. Then you can look at the express controller for a specific view and you will see how it uses populate() (this is pretty much what you will see for a list function in the express controller when you generate a crud module.

Friends.find().sort('-created').populate('user', 'displayName').exec(function(err, friends) {
    // Handle error & return friends code here
});

Hope that helps. You would then want to look at the angular controller and view so that you could modify the object in the controller and reflect it in the view.

BRogers
  • 3,534
  • 4
  • 23
  • 32