I have created a user which has its login data and into it is has some arrays including friends, groups, pages and other stuff.
But I am stucked when I want see who has sent me a friend request.
So I have am trying to create the friend request/response as in Facebook.
My logic it is for the moment when I send you a friend request I add to my array of friends and then with post request I can see at which users is my ID added and it has the status Pending.
For the moment I have tried something but I am getting empty array as response.
My code is below.
user.service.js backend
const User = require("../models/user");
async function findYourPendingContact({friendId}) {
return await User.find({friends: friendId});
}
module.exports = {
findYourPendingContact
}
routes.js
routes.post("/user-friends", UserController.findYourContacts);
This is the Controller.
const userService = require("../service/user.service");
async findYourContacts(req, res, next) {
let friendId = req.body.friendId;
userService.findYourPendingContact({friendId: friendId})
.then(friends => res.json(friends))
.catch(err => next(err));
}
So seems my Database Json.
The user that added me as friend.
{
"_id" : ObjectId("620bfd2d860fd314fc538df3"),
"firstName" : "Max",
"email" : "[email protected]",
"lastName" : "Muster",
"password" : "$2b$10$D8l15CTZHbp/rcApqXpwT.KUbHAg0hBYp0h1qounnKFW.plDO9wKe",
"phone" : {
"number" : "1234566",
"countryCode" : "DE",
"dialCode" : "+49",
"e164Number" : "+491234566",
"internationalNumber" : "+49 1234566",
"nationalNumber" : "1234566"
},
"gender" : 1,
"birthday_day" : "13",
"birthday_month" : "8",
"birthday_year" : "1998",
"friends" : [
{
"friendId" : "6204605617d5fcc3c179f3cc", // this here is my ID
"status" : 0,
"createdDate" : {
"$date" : 1644971031461
}
}
],
"__v" : 0
}
Here are my Data on backend
{
"_id" : ObjectId("6204605617d5fcc3c179f3cc"),
"firstName" : "Test",
"email" : "[email protected]",
"lastName" : "Test",
"password" : "$2b$10$koGb0fIcdWvq2ugGQXF7.OOmoof1QCH8IKQtoMGl9MHSJuu5Xumd6",
"phone" : {
"number" : "01234567",
"countryCode" : "DE",
"dialCode" : "+49",
"e164Number" : "+4901234567",
"internationalNumber" : "+49 01234567",
"nationalNumber" : "01234567"
},
"gender" : 0,
"birthday_day" : "8",
"birthday_month" : "5",
"birthday_year" : "1994",
"__v" : 0
}
Now this is the part of my frontend when I login.
public getUserFriends(userId): Observable<any> {
const api = `${this.baseUrl}/user-friends`
return this.http.post(api, JSON.stringify({friendId: userId}), this.httpOptions).pipe(
map((response: User) => {
console.log(response);
return response;
},
(err) => {
throw err;
})
)
}
user.helper.ts
showPendingFriends(authId): Observable<any> {
return new Observable<any>(observer => {
this.userService.getUserFriends({friendId: authId}).subscribe(response => {
observer.next(response);
})
})
}
components.ts
this.userHelper.showPendingFriends(this.authService.userID).subscribe(res => console.log(res));