我有一個 MongoDB 文檔,它返回某些值,undefined或者null在點表示法中引用時。這是文檔的結構:{ "id": "1", "version": "1.0.0", "categories": [], // Array of Category Objects "bypass": [] // Array of Strings}我使用 Mongoose 如下:Model.findOne({ id: "1" }, { _id: 0 })它成功返回文檔,但是,使用點符號提取特定元素或將它們分配給變量會導致某些元素響應為 null 或未定義。例如,如果我返回的文檔存儲在 中doc,doc.categories將返回[]我創建的類別對象的完整數組 。doc.version返回1.0.0。但是,當我嘗試時doc.bypass,我得到undefined. 如果我嘗試doc.id我得到null.有誰知道發生了什么?代碼編輯:const doc = await Model.findOne({ id: "1" }, { _id: 0 });console.log(doc); // Contains all document data including bypass with its Array of Stringsconsole.log(doc.bypass); // undefined返回:{ id: '1', version: '1.0.0', categories: [ { name: 'Category 1', id: '1111', active: true, children: [Array] }, { name: 'Category 2', id: '2222', active: true, children: [Array] }, { name: 'Category 3', id: '3333', active: true, children: [Array] }, { name: 'Category 4', id: '4444', active: true, children: [Array] } ], bypass: [ '123', '456', '78', '90' ]}undefined
Mongoose 文檔以點表示法返回“未定義”
HUX布斯
2022-10-13 16:45:08