1

edited

Function in the controller:

async postShow(req, res, next) {
        let post = await Post.findById(req.params.id).populate({
            path: 'reviews',
            options: { sort: { '_id': -1 } },
            populate: {
                path: 'author',
                model: 'User'
            }
        });
        let users = await User.find().exec();

        res.render('posts/show', { post, users });

going into the show.ejs page:

 <% users.forEach(function(user) { %>
                        <% console.log(user) %>
                    <% }) %>

this will return the following (from mongoDB) :

  { image: { secure_url: '/images/default-profile.jpg' },
    _id: 5d68ea449ee71014067986e2,
    username: 'john',
    email: '[email protected]',
    __v: 0 },
  { image: { secure_url: '/images/default-profile.jpg' },
    _id: 5d68ef0e3e133417dcc60c64,
    username: 'Tom',
    email: '[email protected]',
    __v: 0 } ]

Now <% post.author %> will return the post author's id like 5d68ef0e3e133417dcc60c64

Thing is, In the ejs template i want to match post.author to user._id and if they match i want it to return user.username of the specific user that was matched. I hope it's a bit more clear now. Thanks again

2
  • 1
    1. No, the log is not an array. Each iteration of the loop simply prints a string. 2. Please post your data (original array of posts and users) so we can help you with some code. Commented Aug 30, 2019 at 11:25
  • I've edited the post accordingly. Commented Aug 30, 2019 at 12:41

1 Answer 1

1

You can use find(): The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

var found = users.find(function(user) {
  return user._id == post.author;
});

This will return the user who's _id property matches the post.author value

Working fiddle here

Sign up to request clarification or add additional context in comments.

6 Comments

I think it should be return user._id == post.author._id assuming that author is the name. Or return user == post.author but this I am not sure if it works in JS
@noname the question says: console.log(post.author) will return a string if it's not in the forEach loop "bye1" so it seems the author property is that string
Sorry it won't help since i'm trying to access it on the front end via EJS template, and users.find afaik won't work . p.s i have edited the post with more details
Can't you just do it before your render and pass it through to the front end?
@BrettGregson yeah right, but in my understanding author mean 'a string' not string which is 'id' and also because he mentioned he using mongodb so I can assume author want to compare user._id with post.author._id
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.