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
requesterproperty is not defined when you attempt to readappeal.requesterbut is added by the time you display theappealobject?requester, which holds another object, has a keyfirstName. However, the shown object above has nofirstNamekey included in theappeals[!sic] property. This is due to the fact, that theappealsproperty 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 theappealsarray in order to get afirstNameIF however the populated data holds such a property itself.