Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Node Express Framework and Javascript not adding to Database

canadianmongoosecanadianmongoose canadaMember Posts: 1
edited April 2016 in JavaScript

I'm trying to add a business in Node Express Framework. My issue is that when I fill out the form to add the business, nothing adds to the page. Been trying to figure out this error for some time now.

My business.js code:

var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');
var Business = require('../models/business');
var passport = require('passport');

// set up the GET handler for the main movies page
router.get('/', function(req, res, next) {
// use the Article model to retrieve all movies
Business.find(function (err, business) {
// if we have an error
if (err) {
console.log(err);
res.end(err);
}
else {
// we got data back
// show the view and pass the data to it
res.render('movies/index', {

            title: 'Business',
            business: business
        });
    }
});

});

// GET handler for add to display a blank form
router.get('/add', function(req, res, next) {
// new
if (req.isAuthenticated()) {
res.render('movies/add', {
title: 'Add a New Business'
});
}
else {
res.redirect('/auth/login');
}

});

// POST handler for add to process the form
router.post('/add', function(req, res, next) {

// save a new article using our Article model and mongoose
Business.create( {
        name: req.body.name,
        city: req.body.city,
        province: req.body.province,
        postal: req.body.postal,
        street: req.body.street
    }
);

// redirect to main business page
res.redirect('/business');

});

// GET handler for edit to show the populated form
router.get('/:id', function(req, res, next) {
// create an id variable to store the id from the url
var id = req.params.id;

// look up the selected article
Business.findById(id,  function(err, business) {
    if (err) {
        console.log(err);
        res.end(err);
    }
    else {
        // show the edit view
        res.render('movies/edit', {
            title: 'Business Details',
            business: business
        });
    }
});

});

// POST handler for edit to update the article
router.post('/:id', function(req, res, next) {
// create an id variable to store the id from the url
var id = req.params.id;

// fill the article object
var business = new Business( {
    _id: id,
    title: req.body.title,
    content: req.body.content,
    date: req.body.date,
    rating: req.body.rating,
    actor: req.body.actor
});

// use mongoose and our Article model to update
Business.update( { _id: id }, business,  function(err) {
    if (err) {
        console.log(err)
        res.end(err);
    }
    else {
        res.redirect('/business');
    }
});

});
//get handler for delete using the article id
router.get('/delete/:id', function(req, res, next){
//grab the id parameter from the url
var id = req.params.id;
Business.remove({ _id: id }, function(err) {
if(err) {
console.log(err);
res.end(err);
}
else {
// show updated business page with redirect
res.redirect('/business');
}
});
});

////auth check
//function isLoggedIn(req, res, next) {
// //is the user authenticated>
// if (req.isAuthenticated()) {
// return next();
// }
// else {
// res.redirect('/auth/login');
// }
//}

// make public
module.exports = router;

My add.ejs code:

<%- include ../partials/header.ejs %>















--
ON
QC
BC
AL
NS
MB
NL
PEI











Save
</form>



Thanks in advance!

Tagged:
Sign In or Register to comment.