I'm trying to get the id of the user who is currently logged in, but I can not find a loop and it does not recover the id of the logged-in user, in order to edit a comment as long as it corresponds to the one who did it and to the user that is currently logged in.
// show view
{{#each storiesView.comments}}
<div class="horizontal">
<div class="card-image">
<p>{{author.name}}</p>
</div>
<p>{{createAt}}</p>
<div class="card-content">
<p>{{message}}</p>
{{idStorie._id}}
<a class="btn-floating btn-small waves-effect waves-light red btn modal-trigger"
href="/comments/edit" data-target="/{{id}}/comments/edit"><i class="fi-pencil"></i></a>
<!-- Modal Structure -->
<div id="/{{id}}/comments/edit" class="modal">
<div class="modal-content">
<form action="/stories/{{idStorie._id}}/comments/{{id}}" method="POST">
<input type="hidden" name="_method" value="PUT">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="message" required>{{message}}</textarea>
<label for="textarea1">Edita el comentario</label>
</div>
<button class="btn waves-effect waves-light green darken-4" type="submit" name="action">Enviar</button>
</form>
</div>
</div>
</div>
<hr>
</div>
{{/each}}
// route
router.get ('/: id', (req, res) = > { Storie.findOne ({ _id: req.params.id }) .populate ('user') .populate ('comments') .then (stories = > {
if(!stories){
req.flash('error_msg', 'la Historia ha sido eliminada ');
res.redirect('/');
}else{
res.render('stories/show',{storiesView: stories,
currentUser: req.user});
}
})
.catch(err =>{
console.log(err);
})
});
// model storie
const StorieSchema = new Schema ({
title:{
type: String,
required: true
},
description:{
type: String,
required: true
},
comments:[{
type: Schema.Types.ObjectId,
ref: 'comments'
}],
user:{
type: Schema.Types.ObjectId,
ref:'users'
},
date:{
type: Date,
default: Date.now
},
});
// model comment const CommentSchema = new Schema ({ message: { type: String, required: true }, createAt: { type: Date, default: Date.now }, author: { userId: { type: Schema.Types.ObjectId, ref: 'users' }, name: String }, idStorie: { type: Schema.Types.ObjectId, ref: 'stories' }
});