Mongodb doesn’t save the data

I’m creating a CRUD with mongodb, and doesn’t throw a error, but when I try to insert the data, the data is not inserted. Someone help me please

App.js

const express = require('express');
const path = require('path');
const morgan = require('morgan');
const mongoose = require('mongoose');

const app = express();

// connecting to db
mongoose.connect('mongodb://localhost/crud', { useUnifiedTopology: true, useNewUrlParser: true })
.then(db => console.log('DB connected'))
.catch(err => console.log(err));

// importing routes
const indexRoutes = require('./routes/index');

// settings
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');  

// middlewares
app.use(morgan('dev'));
app.use(express.urlencoded({extended: false}));

// routes
app.use('/', indexRoutes);

// starting the server
app.listen(app.get('port'), () => {
    console.log(`Server on port ${app.get('port')}`);
});

index.js

    const express = require('express');
    const router = express.Router();
    const Task = require('../models/task');


    router.get('/', async (req, res) => {
      const tasks = await Task.find();
      res.render('index', {
        tasks
      });
    });

    router.post('/add', async (req, res, next) => {
      const task = new Task(req.body);
      console.log(req.body)
      await task.save();
      res.redirect('/');
    })

    module.exports = router;

task.js

      const mongoose = require('mongoose');
      const Schema = mongoose.Schema;

      const TaskSchema = Schema({
          CLiente: String ,
          Correo: String ,
          Direccion: String ,
          Telefono: Number 
      });

      module.exports = mongoose.model('tasks', TaskSchema);

Leave a Reply

Your email address will not be published. Required fields are marked *