0

router.get('/home', ensureAuthenticated, (req, res) => res.render('home', {user_mv: req.user, post_mv: req.post1}));

//Create topic page
router.get('/create-topic', ensureAuthenticated, (req, res) => res.render('create-topic', {user_mv: req.user}));


//Post handle
router.post('/create-topic', (req, res) => {
    const {user_id, title, category, vista, description} = req.body;
    let error2= [];
    if(!title || !category || !vista || !description) {
        error2.push({msg: 'Please check all fields.'});
    };

    if(error2.length > 0){
        res.render('create-topic', {  
            error2,
            title,
            category,
            vista,
            description
        });
    }else {
        //Validation passed
        Post.findOne({title: title})
        .then(post1 => {
            if(post1){
                //User exists
                error2.push({msg: 'Title is already used.'});
                res.render('create-topic', {
                    error2,
                    title,
                    category,
                    vista,
                    description
                });
            } else {
                const newPost = new Post({
                    user_id,
                    title,
                    category,
                    vista,
                    description
                });
                newPost.save()
                    .then(post1 => {
                        res.redirect('/home');
                    })
                    .catch(err => console.log(err));
                };
            });
    };        
});



module.exports = router;
<div class="posts__content">
    <a href="#">
       <h3><%= post_mv.title %></h3>
    </a>
</div>
When logging into my website a user can create a post that inserts into MongoDB with the user's id included. I'm trying to display the title of the 1 post I have in my collection but I get an error that says 'cannot read 'title'property of undefined. Can anyone tell me what I am doing wrong?
Tripiod8
  • 99
  • 6
  • Please use that code

    <%= title %>

    – Mahesh Bhatnagar Jan 12 '20 at 03:14
  • removing the post_mv did not work. It did change the error from 'Cannot read property 'title' of undefined' to 'title is not defined'. – Tripiod8 Jan 12 '20 at 03:34
  • html file name is create-topic ? – Mahesh Bhatnagar Jan 12 '20 at 03:43
  • Please use that code key with value { error2:error2, title:title,category:category, vista:vista, descriptaion:description } – Mahesh Bhatnagar Jan 12 '20 at 03:48
  • the html file name create-topic.ejs. Could you give me an example of what I should write for the code key with value? – Tripiod8 Jan 12 '20 at 04:08
  • I have my newPost object saved as post1. In the router.get('/home') I i have the key value pair as post_mv: post1 which should get me the req.body. The user_mv value pair works but I just added the post_mv so I don't know if I have it set up correctly or if it will work. – Tripiod8 Jan 12 '20 at 04:12
  • I can try. I actually never used github yet but git has been on my list of to-learns – Tripiod8 Jan 12 '20 at 04:13
  • I'm currently setting up my first Github account. I have not used Hangout yet but I probably have an account since I have a google account. Should I focus on Github or Hangout? – Tripiod8 Jan 12 '20 at 04:17
  • You are using setting req.post1 value in post_mv, but are you setting values to req.post1 anywhere ? I guess, you must be setting it from your ensureAuthenticated middleware, if so, please share code for same. – Sarfraaz Jan 13 '20 at 04:50
  • I'm not setting the req.post1 in any middleware. I'm setting post1 as the promise in my newPost.save() at the bottom of my post handle. – Tripiod8 Jan 13 '20 at 05:03

1 Answers1

0

It looks like you are not passing the post to your ejs. So there is no post_mv object, and so it undefined and has no properties.

res.redirect('/home',{post_mv:post1});

I may be wrong, but post1should be the new post object with the title you want as a property returned after saving.

Tumo Masire
  • 422
  • 5
  • 10
  • I added the object to my redirect but unfortunately it did not work. It was a really good idea though and I thought it was gonna work with no problems. You could very well be on the right path though. I just got off work and I didn't sleep well last night and I'm fatigued. I'm gonna try to give it another go when I wake up. I think a problem might be is I have no from or where clauses. For my user_mv im pulling information from a specific user. I am not doing that with the post and just trying to get a random post to display. Thank you for your comment! – Tripiod8 Jan 13 '20 at 22:21
  • Sorry to hear that it didn't work. Are you getting any error messages in the console? And maybe try logging out some data on the server, to see if the variables contain what you think they do when you think they do. – Tumo Masire Jan 14 '20 at 10:26
  • I tried some console logging and googling errors but nothing really helped. I think what might work is using $lookup for querying the collections. – Tripiod8 Jan 14 '20 at 17:25
  • Sometimes i find that (with mongoose), using .then(data => {...}) sometimes doesn't return the data to me? If i use a callback instead of the .then (e.g ``` Post.save((err,data) =>{...}); vs Post.save().then(data => {...}); ```) then sometimes I get different results. Not sure why. not a NodeJS "ninja" or "guru", as HR loves to say. – Tumo Masire Jan 14 '20 at 18:02
  • I did a log of my post1 when submitting and it is returning my data. I found this thread https://stackoverflow.com/questions/6502541/mongodb-query-multiple-collections-at-once which is similar to my setup (I have 2 different files for my schemas). I haven't given a try yet because I'm tired right now and not feeling like I can focus well. I hope to one day become NodeJS ninja because so far this is my favorite type of programming. – Tripiod8 Jan 14 '20 at 18:32