![](/img/trans.png)
[英]JavaScript console can see contents of Object but I can't access it
[英]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.