As I dive into practicing with APIs to hone my skills in creating models and routes, I find myself stuck on getting my initial route to successfully save data to my MongoDB database.
When testing with Postman, I encounter the following error:
{
"message": {
"errors": {
"name": {
"message": "Path `name` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `name` is required.",
"type": "required",
"path": "name"
},
"kind": "required",
"path": "name"
},
"description": {
"message": "Path `description` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `description` is required.",
"type": "required",
"path": "description"
},
"kind": "required",
"path": "description"
}
},
"_message": "Universes validation failed",
"message": "Universes validation failed: name: Path `name` is required., description: Path `description` is required.",
"name": "ValidationError"
}
My code for the model and route sections is as follows:
const mongoose = require('mongoose');
const UniverseSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
description: {
type: String,
required: true
},
date : {
type: Date,
default: Date.now
}
});
//export the route ---------------------Name in db , schema that it should use
module.exports = mongoose.model('Universes', UniverseSchema);
const express = require('express');
const router = express.Router();
const Universe = require('../models/Universe');
// Initial route that will render our universes page
router.get('/', async (req , res) => {
res.send('Universes Page');
try {
const universes = await Universe.find();
res.json(universes);
} catch (error) {
res.json({ message: error });
}
});
// Route used to create a universe
// Create async our post
router.post('/', async (req, res) => {
// Create an instance of the Universe model
const universe = new Universe({
name : req.body.name,
description : req.body.description
});
// Attempt to save our new universe with a try-catch block
try {
const savedUniverse = await universe.save()
res.json(savedUniverse);
console.log('saved');
} catch (error) {
res.json({ message: error});
console.log('Not saved');
}
});
module.exports = router;
When sending data via Postman using a POST request, I include an object with the following structure: { "name":"test1", "description":"test description 1" } Here is my App.js file, which also includes the body-parser setup:
//Server setup
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
require('dotenv').config();
//Middleware
app.use(bodyParser.json());
//index Route
app.get('/' , ( req , res ) => {
res.send('index');
});
// Import routes
const universeRoute = require('./routes/universes');
app.use('/universes', universeRoute );
//Connect to DB
mongoose.connect(process.env.DB_CONNECT,
{ useNewUrlParser: true } ,
() => {
console.log('Connected to DB');
});
app.listen(process.env.PORT || 5000);
All help and insights are greatly appreciated.