Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

javascript - When filtering an array i can suddenly no longer access my document properties?

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...