Validation has failed due to a missing title field in MongoDB

I keep getting the error message

ValidatorError: Path `title` is required
and my data is not getting stored in the database.

What could be causing this issue?

const mongoose=require('mongoose');

const Articleschema= new mongoose.Schema({

    title:{
        type:String,
        required:true
    },
    category:{
        type:String
    },
    content:{
        type:String
    },
    postDate:{
        type:Date,
        default:Date.now()
    }
});

var Article=module.exports=mongoose.model('Article',Articleschema);

router.post('/articles/create',(req,res)=>{
    var article=new Article({
        title:req.body.title,
    })
    article.save(function (err,data){
        if (err) {
            console.log(err)
        }
        console.log(data);
        req.flash('success-message','Article successfully created')
        res.redirect('/admin/articles')
    })
})

Answer №1

If you're encountering an issue, it might be due to not setting up the express middleware to properly handle JSON data in your main index or app.js file.

// To resolve, consider adding the following line of code: app.use(express.json())

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The loading spinner isn't appearing while the function is running

Having trouble displaying a loading spinner while a function is running. I've tried different methods, but the spinner just won't appear. Here's the HTML snippet: <div class="row pt-3" id="firstRow"> <div class="col"> <bu ...

Sending an AJAX request to a Symfony controller's URL

Recently, I encountered an issue with integrating a piece of code from backend.php into my Symfony controller. In the initial setup, I had an AJAX call in a JS file that interacted with backend.php to test some functionality. function postRequest() { var ...

Node.js PDFKit Measurement Units

Can you clarify the measurement unit used in PDFKit (Node.js)? For example, we have: doc.text(20, 20, 'Message') What exactly does 20(x) and 20(x) represent? Are they measured in centimeters, millimeters, inches, or something else? Can I conver ...

Accessing Ionic rootScope within the config stateprovider - A step-by-step guide

Is it possible to access the value of $rootScope in the following line? .config(function($stateProvider, $urlRouterProvider) { $stateProvider // I am trying to retrieve the value of $rootScope here. } ...

An error popped up while using the fetch API due to an unexpected end of input

fetch('http://www.freegamesforyourwebsite.com/feeds/games/?tag=platform&limit=100&format=json', { method:'GET', mode:'no-cors', dataType: 'json', headers: { 'Accept': 'a ...

Introduction to Angular controller binding for beginners

I'm currently going through this tutorial : http://www.youtube.com/watch?v=i9MHigUZKEM At 46:32 minutes into the tutorial, this is the code I have implemented so far : <html data-ng-app="demoApp"> <body data-ng-controller="SimpleControlle ...

What is the best way to send a JSON object to bootstrap-table?

In my controller, I am passing a JSON encoded object to the view. Using a Bootstrap table in the view to display the data, but it is showing "No matching records found." Can someone please assist with this issue? Here is my controller: see image And here ...

Is there a way for me to send a URL request from my website to a different server using PHP?

I am looking to initiate a request from my website to my server using a specific IP address. From there, I need the server to send another request to a different server and then display the response on the webpage. Can anyone provide guidance on how to ...

Ways to alter the div post user authentication

I'm in the process of developing a website with a single-page application setup. I'm utilizing Node.js for the backend and Angular for the frontend. The challenge I'm facing is displaying a specific div when a user is not logged in, and swit ...

Can someone explain the integration of socket.io and express in a simple way?

I am encountering an issue with socket.io emit and express. My goal is to emit data for a web game, specifically to send players data to a specific lobby. For instance, if I create a lobby at the following link: localhost:8000/test/lobbyName, I want to emi ...

JavaScript AJAX functions work well when using jQuery, but they may not function properly

Here is the code snippet I am working with: Controller Section def some_method respond_to do |format| format.js { render js: "alert();" } end end JavaScript Section The code above triggers an alert(); jQuery.ajax({ url: url }); However, the ...

Using JavaScript, import the variable module object from a specific module

Is there a way to import a module object from a module if I am unsure of the specific objects I will need beforehand? How can I utilize a variable in place of fixed module names? import { dynamicVariable } from './module' The variable represents ...

Text centered vertically with jQuery is only loaded after the page is resized

My jQuery script is designed to vertically center text next to an image, but it seems to only work properly after resizing the page. $(window).load(function () { $(function () { var adjustHeight = function () { $('.vertical-al ...

Utilizing Directives for DOM Manipulation in AngularJS

At this moment, I have a functional Angular app that is working properly. However, I am currently performing DOM manipulation within my controller instead of utilizing directives as recommended. My concern is, how can I correctly implement this functionali ...

What is the most effective way to show the current date on a webpage: utilizing JavaScript or PHP?

Looking to show the current date and time on a webpage. There are two potential methods to achieve this: (1) Using PHP, for example: <?php echo date('Y-m-d H:i:s');?> (2) ... or JavaScript, like so: <div> <script>document.wri ...

Angular 2+ Service for tracking application modifications and sending them to the server

Currently I am facing a challenge in my Angular 4 project regarding the implementation of the following functionality. The Process: Users interact with the application and it undergoes changes These modifications are stored locally using loca ...

Setting up mongoid for Ruby programming: A step-by-step guide

I am attempting to work with Ruby without using Rails, but I would like to incorporate the Mongoid gem to access its various methods. Despite installing the Mongoid gem and setting up all the necessary components for Mongoid, my Ruby application is still ...

Modify the color of a set of div elements when clicked

Is there a way to change the background color of all divs with a common attribute value (#id for example) after clicking on one of the divs that shares the same attribute value? This is what I have tried so far: $('.group').click(function(){ ...

Creating multiple asynchronous calls within a loop in JavaScript

I am currently working on a task in my gulpfile.js that involves uploading an app using Gulp and SharePoint. 'use strict'; const gulp = require('gulp'); const build = require('@microsoft/sp-build-web'); const spsync = require ...

Discover the world of Google Chrome Apps with the power of chrome.storage.local

While building an application, I encountered a perplexing issue that I need help with: The task involves reading a JSON file and storing its content in localStorage using chrome.storage.local in a Chrome app. Here is a snippet from the JSON file: { "s ...