簡體   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