0

I have a reservation model in my database. This is how I fetch one entry (Using mongoose),

reservationModel.get_reservation_by_user_id(payload.user.id, function (err, reservations) {
      if (err) {
        console.log(err);
      } else {
        console.log(reservations);
        console.log(reservations[0].user);  // prints undefined                         
        console.log(reservations[0].from); // prints 0900
      }
    });

Here is the console output.

Dialog Opened sucessful
[ { _id: 5a887a20734d1d041bb6a1f3,
    tutor: '5a760a1f734d1d3bd58c8d52',
    user: 'U8XDVJD26',
    date: 2018-02-17T13:53:45.415Z,
    day: 'Saturday',
    from: '0900',
    to: '1030',
    active: 'no' },
  { _id: 5a887bd2734d1d041bb6a24f,
    tutor: '5a760a1f734d1d3bd58c8e12',
    user: 'U8XDVJD26',
    date: 2018-02-17T14:00:45.415Z,
    day: 'Saturday',
    from: '0930',
    to: '1130',
    active: 'no' } ]
undefined
0900

How can I get the value of user?

M.Shaikh
  • 250
  • 1
  • 3
  • 14

2 Answers2

0

If you want to get a record by id you need to use a method findById. Example:

reservationModel.findById(payload.user.id, function (err, reservations) {
      if (err) {
        console.log(err);
      } else {
        console.log(reservations);
        console.log(reservations.user);                     
        console.log(reservations.from);
      }
    });
Aleksei Korkoza
  • 427
  • 7
  • 22
  • I have written a logic to find reservation in my own function `get_reservation_by_user_id` and when I log the response it shows fine as I displayed that in the output as well. So I dont think the problem is with the method call. – M.Shaikh Feb 17 '18 at 21:00
  • I understood you. Sorry for wrong answer. – Aleksei Korkoza Feb 17 '18 at 21:02
0

I had 'user_id' written in my schema and not 'user', this was the error.

M.Shaikh
  • 250
  • 1
  • 3
  • 14