skip to Main Content

i’m new to nodeJS. am trying a use post request to get the information the user post but am getting an error: TypeError: Cannot read properties of undefined (reading ‘title’). please what am i doing wrong here.

here’s my code

const express = require("express")
const app = express()
const mongoose = require("mongoose")
const Schema = mongoose.Schema;

const BlogPost = new Schema({
    title:{
        type: String,
        required: true
    },
    content: {
        type: String,
        required: true
    },
    body: {
        type: String,
        required: true
    }
})

const Blog = mongoose.model("Blog", BlogPost)
module.exports = Blog;

app.post("/blogs", (req, res) => {
    const blogs = new Blog({
        title:req.body.title,
        content: req.body.content,
        body: req.body.body,
    })
    blogs.save()
    .then(result => console.log(result))
    .catch(err => console.log(err))
})

2

Answers


  1. You may not get title from req.body.title that’s why when you save this doc to mongodb it will throw this error.

    For solution, you must check req.body.title before saving data into mongodb,
    Otherwise, simply REMOVE required: true from schema

    title:{
        type: String,
        required: true
    },
    
    Login or Signup to reply.
  2. To make this code to workable condition for that add app.use(express.json())

    As we are accepting http request body value from postman , so that it will parse our http request into json payload,

    Hence we can access the body parameters value by req.body..

    const express = require("express")
    const app = express()
    const mongoose = require("mongoose")
    const Schema = mongoose.Schema;
    
    // 2 json parsing
    app.use(express.json())
    
    //as my mongoose version is latest one so in that strictQuery is not supported
    mongoose.set('strictQuery', false);
    
    const BlogPost = new Schema({
        title: {
            type: String,
            required: true
        },
        content: {
            type: String,
            required: true
        },
        body: {
            type: String,
            required: true
        }
    })
    
    const Blog = mongoose.model("Blog", BlogPost)
    module.exports = Blog;
    
    //3. connection bridge between applicaion and mongoose
    const connectionUrl = 'mongodb://127.0.0.1:27017'
    mongoose.connect(connectionUrl, {
        useNewUrlParser: true
    },()=>{
        console.log('Connected to MongoDB')
    })
    
    
    //Middleware
    
    app.post("/blogs", (req, res) => {
        //console.log(req.body.title+ "  "+ req.body.content+ "  "+ req.body.body );
        const blogs = new Blog({
            title: req.body.title,
            content: req.body.content,
            body: req.body.body,
        })
        blogs.save()
            .then(result => console.log(result))
            .catch(err => console.log(err))
    })
    
    // 1.listen 
    app.listen(3033, () => {
        console.log("Server is running on port 3033")
    });
    
    /**
     * use post request to get the information 
     * the user post but getting an error: 
     * TypeError: Cannot read properties of undefined (reading 'title').
     */

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search