Friend Request System with Express & MongoDB Friend Request System with Express & MongoDB express express

Friend Request System with Express & MongoDB


We would need to take one schema only which is userSchema(as is Israel said above, you only need an array/object to list your friendship on the userSchema). But we will need to add another schema(said it friendRequestSchema).

FriendRequest schema would be:- ID user request (int)- ID user recipient (int)- status (int) //let say 1= requested, 2=accepted, 3=rejected

And the controller it should be from the user A click "Friend Request" button on your user B page.

  1. Friend Request Button will call a function let saying it "sendFriendRequest()"

If function running it would be recorded on friendRequest DB, which is will record ID of user A(as requester), ID of user B and request status.

If request status = 1 then user B will be notified and give him two option which is accepted and rejected.

  1. User B accept or reject it

If user press button accept, then the status updated in friendRequest DB to be=> 2 (Accepted). Then, you have to call another function to add user ID A to friendship list of User B. Conversely. Then if you want to make a notification you can call it as well.

Else user B will press reject (status will be => 3) then notif it.

UserSchema

var UserSchema = new Schema({name: String,username: {    type: String,    index: true},password: {    type: String,    required: true},email: String,  friendship: [String] });

Then FriendRequestschema

var FriendRequestSchema = new Schema({requester: {    type: int,    required: true},recipient: {    type: int,    required: true},status:     type: int,    required: true });

This just to let you know, how its work. More complex method about (sendrequest,block .etc) you can check this link, It's flow process for PHP, but you can easily move it to your js. Hope it help you man.


Your model can be improved, and your code can be cleaned:

First, you don't need the brackets if you only give type for the field:

var UserSchema = new Schema({    name: String,    username: {        type: String,        index: true    },    password: {        type: String,        required: true    },    email: String,      friends: [String]});

This should be a simplified version of your schema. The _id field doesn't need to be specified because mongoose creates it automatically. If you wanna put a customized value there, just do it when you insert.

Second:

If you wanna reference other users, why not to use only a simple array that contains ids from other users. For example, if you have user A, the "friendship" of this user are user ids contained in his "friends" field.

{id:12345, username:"A", password:***, email:"a@fakemail.com", friends:[B_id,C_id,D_id]}

In that case, whenever you wanna make a list of friends of A, you can just perform a $lookup operation in mongodb and it will fill the other users information for you.

I don't think I covered all of your questions, but I hope my answer was helpful.