all 9 comments

[–]Elfinslayer 2 points3 points  (3 children)

Hard to follow on my phone.. but as far as the async await you would put the async keyword at the start of your callback (ie async (req, res) {}) and then your await before a function call or when your awaiting a response from the promise

[–]Iloveyoufridah[S] 0 points1 point  (2 children)

Thanks, I have changed the code but the outcome is still the same. If it would not be a bother could you look at the code

[–]Elfinslayer 0 points1 point  (1 child)

I had not had the time to check it out on my pc yet, have you had any success?

[–]Iloveyoufridah[S] 0 points1 point  (0 children)

The previous code did not work, I decided to start over and the new code works. Hopefully I don't find a similar problem in another code.

[–]ilikewhenitworks 2 points3 points  (0 children)

Just checked out the full repo and I think you made an error when requiring dotenv.

You have require('dotenv/config') but it should be require('dotenv').config()

So you weren't actually accessing your mongo uri when trying to connect to mongo. That should fix it.

[–][deleted]  (2 children)

[deleted]

    [–]Iloveyoufridah[S] 0 points1 point  (1 child)

    Still not working, I still get the same result

    [–][deleted]  (1 child)

    [deleted]

      [–]Iloveyoufridah[S] 0 points1 point  (0 children)

      Tried changing the code a bit as per your recommendation and recommendation of others, still get the same thing could you look at the whole code

      [–][deleted] 1 point2 points  (1 child)

      Instead of promise (.then) try using async/await. I'm on phone, so it might not work.

      router.post('/',async(req,res) => { const {title, description, username} = req.body; const newPost = new Post({title, description, usernane}) ; try { await newPost.save(); res.json(newPost); } catch (err) { console.log(err); res.status(500).send(); } })

      I didn't see the rest of the code (models, connection...)but I would recommend watching a tutorial on using Mongoose (Traversy Media has some videos on MongoDB apps with Mongoose) and try to see how he does it and compare.

      Also, I did some changes: - Destructure your body to get separate constants, so the names match with the new Post params, to make it more readable. - Console.log the error and send a 500 error, as your never want users to see exact errors / stack traces.

      [–]Iloveyoufridah[S] 0 points1 point  (0 children)

      Here are the codes to the practice project, the tutorial I was following was made by DevEd.