It's strange. Or maybe not. But when I filter through an array of MongoDB documents, for example using
...
let factsAll = await Fact.find({ });
let facts = [], factsTemp = [];
subscribed.forEach(user => {
factsTemp = factsAll.filter(e => e.author.uuid === user.uuid);
facts.push(factsTemp);
});
All inside an async
function of course. When subscribed.length === 1
, ie. when the forEach only runs once, it works for some reason. However when its more than one i get a returned list where i can no longer access my properties, as I would do with facts[n].author.uuid
for example, and I get the following error: Cannot read property uuid
of undefined
Is there something weird going on in my code, mongoose or am i just dumb?
Here's my mongoose Model
...
const FactSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
content: {
type: String,
required: true
},
approvals: {
type: Number,
default: 0
},
date: {
type: Date,
default: Date.now()
},
author: {
name: {
type: String,
default: 'Anonymous'
},
uuid: {
type: String,
default: '#'
},
}
});
...
Thanks in advance :D
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…