Seqequalize error can anyone please tell me how to insert into database using form in Node

Posted 10 months ago by asad98iftikhar

var express=require('express'); var router=express.Router(); var expressValidator = require('express-validator'); var bodyParser=require('body-parser');

//Get Page model var Page=require('../models/page');

/*

  • GET PAGES INDEX */

router.get('/',function(req,res){ res.send('admin area'); }); router.get('/admin/dashboard',function(req,res){ res.render('admin/dashboard'); });

/* ** GET ADD PAGE */ router.get('/add-page',function(req,res){ var title=""; var slug=""; var content="";

res.render('admin/add_page',{ title:title, slug:slug, content:content

}); }); //POST ADD page router.post('/add-page',function(req,res){

req.checkBody('title','Title must have a body.').notEmpty(); req.checkBody('content','Content must have a body.').notEmpty();

var title=req.body.title; var slug=req.body.slug.replace(/ \s+/g, '-').toLowerCase(); if(slug==" ")slug=title.replace(/ \s+/g, '-').toLowerCase();

var content=req.body.content;

var errors=req.validationErrors();

if(errors){ res.render('admin/add_page',{ errors:errors, title:title, slug:slug, content:content }); } else{ Page.findOne({slug:slug},function(err,page){ if(page){ req.flash('danger','Page slug Already Exist,'); res.render('admin/add_page',{

        title:title,
        slug:slug,
        content:content
      });
    }
    else{
        var Page=new Page({
          title:title,
          slug:slug,
          content:content,
          sorting:0
        });
        page.save(function(err){
          if(err) return console.log(err);

          req.flash('success','Page Added Successfully!!');
          res.redirect('/admin/pages');
        });
    }
  });

}

}); //Exports module.exports=router;

//This is my admin_page

'use strict'; module.exports = (sequelize, DataTypes) => { var page = sequelize.define('page', { title: DataTypes.STRING, slug: DataTypes.STRING, content: DataTypes.STRING, sorting:DataTypes.INTEGER }, {}); page.associate = function(models) { // associations can be defined here }; return page; };

//This is my Page Model

{ "name": "gaffercart", "version": "1.0.0", "description": "\u001b[A\u001b[A\u001b[B\u001b[BE-Commerce shopping Cart Made By Gaffer", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "body": "^5.1.0", "connect-flash": "^0.1.1", "ejs": "^2.5.8", "express": "^4.16.3", "express-messages": "^1.0.1", "express-session": "^1.15.6", "express-validator": "^5.1.2", "mustache-express": "^1.2.5", "mysql": "^2.15.0", "mysql2": "^1.5.3", "pg": "^7.4.1", "sequelize": "^4.37.6", "sequelize-cli": "^4.0.0", "sequelize-handlers": "^1.0.6" } }

//This is my Package.json

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.