当对象在另一个对象内时访问对象属性

时间:2018-05-31 00:19:48

标签: node.js mongodb mongoose mongoose-schema

我正在使用javascript / mongoose。我有以下mongoose / mongodb申诉对象

{
  _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: 'jacab.@gmail.com',
    birthday: '0022-02-22',
    meetingPoints: 2,
    role: 'member',
    __v: 26
  },
  __v: 0
}

我正在尝试读取申诉人属性/对象内的firstName属性,该属性位于申诉对象内。所以firstName < requester < appeal。我输入了这个:name = appeal.requester.firstName但我收到以下错误:Cannot read property 'firstName' of undefined。有什么想法吗?

这是我的架构:

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"
    },
});

注意:我已经占据了请求对象

0 个答案:

没有答案