Ich habe ein Benutzerschema und ein Postschema, wobei ein Benutzer viele Beiträge hat. Ich möchte alle Posts zurückgeben, die der Benutzer auf einer Route mit dem Namen '/ post/dashboard' hat.Mongoose finde Dokumente mit Feld = req.body.user
Hier ist meine Schemata:
let UserSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
default: null,
},
profile_pic: {
type: String,
default: '/img/profilepic.png',
},
posts: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Post'
}
})
let PostSchema = new Schema({
title: {
type: String,
},
description: {
type: String,
}
original_poster: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
tags: {
type: [String]
}
})
So zum Beispiel so etwas wie:
app.get('/', (req,res) => {
Post.find({ original_poster: req.session.user }).then((posts) =>{
res.send(JSON.stringify(posts));
}) //where req.session.user is an id (the logged in user's object id or _id)
})
Im Wesentlichen wie etwas in SQL-Syntax es sein könnte:
SELECT * FROM POSTS WHERE ORIGINAL_POSTER = <req.session.user>
Was das ist richtige Möglichkeit, alle Beiträge von der req.session.user zurückzugeben?