title: Mongoose Schema show the array if a friendId exist in Users Model schema
body: <p>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.</p>
  <p>User model</p>
  <pre><code>const mongoose = require('mongoose');
  const uniqueValidator = require('mongoose-unique-validator');
  
  const userDataScheme = new mongoose.Schema({
      firstName: String,
      lastName: String,
      email: {
          type: String,
          unique: true
      },
      password: String,
      phone: {
          number: String,
          countryCode: String,
          dialCode: String,
          e164Number: String,
          internationalNumber: String,
          nationalNumber: String
      },
      gender: Number,
      birthday_day: String,
      birthday_month: String,
      birthday_year: String,
      friends: [{
          _id: false,
          friendId: String,
          createdDate: { type: Date, default: Date.now },
          status: Number,
      }
      ]
  },
      { autoIndex: false });
  userDataScheme.plugin(uniqueValidator, { message: 'Email already in use.' });
  module.exports = mongoose.model('User', userDataScheme);
  </code></pre>
  <p>user.service.js backend</p>
  <pre><code>const User = require(&quot;../models/user&quot;);
  async function findYourPendingContact({friendId}) {
      return await User.find({friends: friendId});
  }
  
  module.exports = {
      findYourPendingContact
  }
  </code></pre>
  <p>routes.js</p>
  <pre><code>routes.post(&quot;/user-friends&quot;, UserController.findYourContacts);
  </code></pre>
  <p>This is the Controller.</p>
  <pre><code>const userService = require(&quot;../service/user.service&quot;);
  async findYourContacts(req, res, next) {
      let friendId = req.body.friendId;
      userService.findYourPendingContact({friendId: friendId})
      .then(friends =&gt; res.json(friends))
      .catch(err =&gt; next(err));
  }
  </code></pre>
  <p>So seems my Database Json.
  The user that added me as friend.</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;620bfd2d860fd314fc538df3&quot;),
      &quot;firstName&quot; : &quot;Max&quot;,
      &quot;email&quot; : &quot;maxmuster@outlook.de&quot;,
      &quot;lastName&quot; : &quot;Muster&quot;,
      &quot;password&quot; : &quot;$2b$10$D8l15CTZHbp/rcApqXpwT.KUbHAg0hBYp0h1qounnKFW.plDO9wKe&quot;,
      &quot;phone&quot; : {
          &quot;number&quot; : &quot;1234566&quot;,
          &quot;countryCode&quot; : &quot;DE&quot;,
          &quot;dialCode&quot; : &quot;+49&quot;,
          &quot;e164Number&quot; : &quot;+491234566&quot;,
          &quot;internationalNumber&quot; : &quot;+49 1234566&quot;,
          &quot;nationalNumber&quot; : &quot;1234566&quot;
      },
      &quot;gender&quot; : 1,
      &quot;birthday_day&quot; : &quot;13&quot;,
      &quot;birthday_month&quot; : &quot;8&quot;,
      &quot;birthday_year&quot; : &quot;1998&quot;,
      &quot;friends&quot; : [
          {
              &quot;friendId&quot; : &quot;6204605617d5fcc3c179f3cc&quot;, // this here is my ID
              &quot;status&quot; : 0,
              &quot;createdDate&quot; : {
                  &quot;$date&quot; : 1644971031461
              }
          }
      ],
      &quot;__v&quot; : 0
  }
  </code></pre>
  <p>Here are my Data on backend</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;6204605617d5fcc3c179f3cc&quot;),
      &quot;firstName&quot; : &quot;Test&quot;,
      &quot;email&quot; : &quot;test.test@outlook.com&quot;,
      &quot;lastName&quot; : &quot;Test&quot;,
      &quot;password&quot; : &quot;$2b$10$koGb0fIcdWvq2ugGQXF7.OOmoof1QCH8IKQtoMGl9MHSJuu5Xumd6&quot;,
      &quot;phone&quot; : {
          &quot;number&quot; : &quot;01234567&quot;,
          &quot;countryCode&quot; : &quot;DE&quot;,
          &quot;dialCode&quot; : &quot;+49&quot;,
          &quot;e164Number&quot; : &quot;+4901234567&quot;,
          &quot;internationalNumber&quot; : &quot;+49 01234567&quot;,
          &quot;nationalNumber&quot; : &quot;01234567&quot;
      },
      &quot;gender&quot; : 0,
      &quot;birthday_day&quot; : &quot;8&quot;,
      &quot;birthday_month&quot; : &quot;5&quot;,
      &quot;birthday_year&quot; : &quot;1994&quot;,
      &quot;__v&quot; : 0
  }
  </code></pre>
  <p>Now this is the part of my frontend when I login.</p>
  <pre><code> public getUserFriends(userId): Observable&lt;any&gt; {
      const api = `${this.baseUrl}/user-friends`
      return this.http.post(api, JSON.stringify({friendId: userId}), this.httpOptions).pipe(
        map((response: User) =&gt; {
          console.log(response);
          return response;
        },
        (err) =&gt; {
          throw err;
        })
      )
    }
  </code></pre>
  <p>user.helper.ts</p>
  <pre><code>  showPendingFriends(authId): Observable&lt;any&gt; {
      return new Observable&lt;any&gt;(observer =&gt; {
        this.userService.getUserFriends({friendId: authId}).subscribe(response =&gt; {
          observer.next(response);
        })
      })
    }
  </code></pre>
  <p>components.ts</p>
  <pre><code> this.userHelper.showPendingFriends(this.authService.userID).subscribe(res =&gt; console.log(res)); //this.authService.userID is my ID actually 6204605617d5fcc3c179f3cc or it can be any user ID
  </code></pre>