繁体   English   中英

无法在 javascript 中访问子对象,但我看到了

[英]Can't access child object in javascript, nevertheless I see it

我有一个从 mongo db 返回的 javascript 对象的问题,如果我 console.log 父对象,我看到子对象,如果 console.log 或使用子对象,我得到未定义

const templateResponse = await this.dbService.findbyCode(req);  // <= here I get the parent object

console.log('EmailService -> getEmailCOntent -> templateResponse', templateResponse); 

if (templateResponse) {
console.log('EmailService -> getEmailCOntent -> templateResponse6666', templateResponse.email);
}

EmailService -> sendMails -> emailContent 

在这里我 console.log 父,我看到子电子邮件对象:

{ _id: 5f5421eaa9248b90c815d80b,
  email:
   { to: 'email@mail.com',
     cc: '',
     bcc: '',
     subject: 'testttt',
     body: 'this is a test',
     from: 'no-reply@test.com' },
  code: 'REQUESTOREMAILNOACCESS',
  name: '',
  description: '',
  __v: 0
 }

如果我尝试 console.log 子对象我得到未定义,我也试过templateResponse['email']

EmailService -> getEmailCOntent -> templateResponse.email 
undefined

所以我通过在 mongo find 函数的末尾简单地添加 .lean() 来解决这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM