Answer the question
In order to leave comments, you need to log in
How to pass object to list of mongoose objects?
There is a user model
const UserSchema = mongoose.Schema({
name: {
type: String
},
email: {
type: String,
required: true
},
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
posts: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Post'
}]
});
var PostSchema = new Schema({
post: {
type: String,
required: true
},
author: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
createdAt: {type: Date, default: Date.now},
likes: {type: Number, default: 0}
});
router.post('/create-post', passport.authenticate('jwt', {session:false}), function (req, res, next) {
var post = new Post();
post.post = req.body.post;
post.author = req.user._id;
post.save(function (err) {
if (err)
res.send(err);
res.json({success: 'Post created ID:' + post._id, postedBy: post.author});
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question