I am on a computing course and have an issue trying to insert data into mongodb. we are using visual studio code and node js. When i post data using postman i dont get any return result and no data gets inserted into db. I have literally gone over the code a hundred times and the lecturer has even given me his code which mine matches. yet his gets a result and mine doesnt. we cannot go into university atm and i just cant get help online so have come here! can someone advise what is going wrong pls?
Here is my code:
app.js
const express = require('express')
const app = express()
const mongoose =require('mongoose')
require('dotenv/config')
const bodyParser = require('body-parser')
const postsRoute = require('./routes/posts')
app.use(bodyParser.json())
app.use('/posts',postsRoute)
app.get('/', (req,res) =>{
res.send('Homepage')
})
mongoose.connect(process.env.DB_CONNECTOR, ()=>{
console.log('DB is now connected!')
})
app.listen(3000, ()=>{
console.log('Server is up and running...')
})
posts.js (Sits in the routes folder:
const express =require('express')
const router = express.Router()
const Post = require('../models/Post')
// POST (Create data)
router.post('/',async(req,res)=>{
//console.log(req.body)
const postData = new Post({
user:req.body.user,
title:req.body.title,
text:req.body.text,
hashtag:req.body.hashtag,
location:req.body.location,
url:req.body.url
})
// try to insert...
try{
const postToSave = await postData.save()
res.send(postToSave)
}catch(err){
res.send({message:err})
}
})
// GET 1 (Read all)
router.get('/', async(req,res) =>{
try{
const getPosts = await Post.find().limit(10)
res.send(getPosts)
}catch(err){
res.send({message:err})
}
})
// GET 2 (Read by ID)
router.get('/:postId', async(req,res) =>{
try{
const getPostById = await Post.findById(req.params.postId)
res.send(getPostById)
}catch(err){
res.send({message:err})
}
})
// PATCH (Update)
router.patch('/:postId', async(req,res) =>{
try{
const updatePostById = await Post.updateOne(
{_id:req.params.postId},
{$set:{
user:req.body.user,
title:req.body.title,
text:req.body.text,
hashtag:req.body.hashtag,
location:req.body.location,
url:req.body.url
}
})
res.send(updatePostById)
}catch(err){
res.send({message:err})
}
})
// DELETE (Delete)
router.delete('/:postId',async(req,res)=>{
try{
const deletePostById = await Post.deleteOne({_id:req.params.postId})
res.send(deletePostById)
}catch(err){
res.send({message:err})
}
})
module.exports = router
Post.js (data model) sits in the models folder:
const mongoose = require('mongoose')
const PostSchema = mongoose.Schema({
user:{
type:String,
required:true
},
title:{
type:String,
required:true
},
text:{
type:String,
required:true
},
hashtag:{
type:String,
required:true
},
location:{
type:String,
required:true
},
url:{
type:String,
required:true
},
date:{
type:Date,
default:Date.now
}
})
module.exports = mongoose.model('posts',PostSchema)
Here is the data format I am putting into postman :
{
“user”: “Safi”,
“title”: “mr”,
“text”: “Hello, this is my first message”,
“hashtag”: “#VideoGames”,
“location”: “london”,
“url”: “htpps://safi.com”
}
I am meant to get a response on postman with the data and a unique id. however nothing is returned and nothing is inserted into mongodb.
a JSON package is definitely being created as (even though it is commented out) I have a line //console.log(req.body) that when I “uncomment” it the terminal on VS shows the JSON data.
I have tried on another pc and get the same issue.