Hi
Ich kann normal einen Post erstellen. Ich erstelle es mit einem Autorisierungs-JWT-Token, mein Forum Schema hat ein CreatedBy Attribut, sodass in meinem Token auch meine userID enthalten ist.
Ich möchte nur wissen, wie ich mit JWT Token einmal alle Posts finden kann, die von Admin erstellt wurden und zweitens, die von einem anderen Benutzer erstellt wurden.
Ich habe zwei HTTP-Anfragen. Zuerst:
GET http://localhost:8080/forum/getpostsadmin
Authorization: Bearer {token}
exports.getByToken = async(req,res) => {
Forum.find({_id: req.params._id}, function(err, docs) {
if (err) {
res.json(err);
}
else {
res.json(docs);
}
});
};
GET http://localhost:8080/forum/getOwner
Authorization: Bearer {token}
Content-Type: application/json
{
"ownerID": "another User"
}
exports.getByOwnerID = async (req, res, next) => {
Forum.find({ createdBy: req.body.createdBy })
.then(doc => {
if(!doc) { return res.status(400).end();}
return res.status(200).json(doc);
})
.catch(err => next(err));
}
Forum Schema:
const forumSchema = ({
forumName: {
type: String,
required: true,
},
forumDescription: {
type: String,
required: true,
},
createdBy: {
type: Schema.Types.ObjectId, ref: 'User'
},
published_on: {
type: String,
default: moment().format("LLL")
},
});
Falls weitere Fragen offen sind würde ich SIe beantworten.