0

I am using javascript/ mongoose. I have the following mongoose/mongodb appeal object

{
  _id: 5b0f36266a4dcb1cb8feebd6,
  eventName: 'General Meeting',
  eventDate: 2018-12-22T00:00:00.000Z,
  eventType: 'DMs',
  reason: 'Emergency',
  reasonDescription: 'My dog passed away',
  submissionDate: '30/5/2018 @ 18:39:18',
  requester: {
    events: [5b079c109f2526395a2a8867, 5b0919a81d593006691d5c29, 5b0919c41d593006691d5c2a],
    wentEvents: [5b079bcb9f2526395a2a8865, 5b079bcb9f2526395a2a8865, 5b079bcb9f2526395a2a8865, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28],
    programmingEvents: [5b079bcb9f2526395a2a8865, 5b079bcb9f2526395a2a8865, 5b079bcb9f2526395a2a8865],
    socialEvents: [],
    serviceEvents: [5b0919901d593006691d5c28, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28, 5b0919901d593006691d5c28],
    appeals: [5b0bcd7c91ce990a289eba91, 5b0f34f3aab8a41c61fe347b, 5b0f36266a4dcb1cb8feebd6, 5b0f3be76787d51fc065a363, 5b0f3dcd7e60351feb17ca89],
    _id: 5b092f18ada6ba0f83cb8fdd,
    username: '666666666',
    major: 'French Studies',
    phoneNumber: '6823316625',
    subcommittee: 'SHIP',
    firstName: 'Jacob',
    lastName: 'Mullen',
    email: '[email protected]',
    birthday: '0022-02-22',
    meetingPoints: 2,
    role: 'member',
    __v: 26
  },
  __v: 0
}

I am trying to read the firstName property that is inside the requester property/object which is inside the appeal object. So firstName < requester < appeal. I have typed this: name = appeal.requester.firstName but I get the following error: Cannot read property 'firstName' of undefined. Any ideas why?

Here is my Schema:

var AppealSchema = new mongoose.Schema({
    eventName: String,
    eventDate: Date, 
    eventType: String,
    reason: String,
    reasonDescription: String,
    submissionDate: String,
    staffDecision:String,
    requester: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    },
});

NB: I HAVE POPULATED THE REQUESTER OBJECT

7
  • How and where are you displaying the object above? Could it be the requester property is not defined when you attempt to read appeal.requester but is added by the time you display the appeal object? Commented May 31, 2018 at 0:25
  • first that is not a valid js object (missing some quotes) and second nothing wrong with the way that you trying to get first name, the error indicates that appeal object doesn't have any property called requester, maybe you have typo? Commented May 31, 2018 at 0:25
  • 2
    Show you schema as it's likely different to what the document is. Please be careful tagging these questions as JavaScript, because it often "looks like" something else to those who don't understand mongoose. Commented May 31, 2018 at 0:26
  • I edited my comment but it's not valid js object either Commented May 31, 2018 at 0:27
  • When looking at this rather invalid JavaScript object, I can only see that the key requester, which holds another object, has a key firstName. However, the shown object above has no firstName key included in the appeals[!sic] property. This is due to the fact, that the appeals property holds an array of strings, which are some unpopulated ids from your mongo database. You have to populate all the ids which are located in the appeals array in order to get a firstName IF however the populated data holds such a property itself. Commented May 31, 2018 at 0:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.