1

This is my Schema which is in models/adder folder:

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


var schema = new Schema({
title: { type: String, required: true },
image: { type: String, required: true },
url: { type: String, required: true }
});
module.exports = mongoose.model('Product', schema);

Now this is my index.js where all the routes have been made for get, post and delete:

var express = require('express');
var router = express.Router();
var Product = require('../models/adder');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.createConnection('localhost:27017/replica');

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('partials/index', { title: 'Express' });
});
router.get('/adder', function(req, res, next) {
res.render('adder/adder')
})
router.post('/added', function(req, res, next) {
var products = new Product({
    title: req.body.title,
    image: req.body.image,
    url: req.body.url
});
products.save(function(err, done) {
    if (err) {
        console.log(err);
    } else {
        console.log('that have been saved')
    }
    res.redirect('/');
});

});

 module.exports = router;

Now whenever i am submiting the fourm products have all the data i entered I had consoled it and i am able to make conenction with my local mongoDB server. But i am not able to use save function. Can anyone guide me how to save the data which is in products .

Ashish sah
  • 755
  • 9
  • 17

0 Answers0