3

This is my admin_pages.js file i have done the migrations and models but i am getting this error.

TypeError: Cannot read property 'findOne' of undefined at C:\users\gaffer\desktop\gaffercart\routes\admin_pages.js:80:21 at Layer.handle [as handle_request] (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\layer.js:95:5) at next (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\layer.js:95:5) at C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:335:12) at next (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:275:10) at Function.handle (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:174:3) at router (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:317:13) at C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:335:12) at next (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:275:10) at C:\users\gaffer\desktop\gaffercart\index.js:70:3 at Layer.handle [as handle_request] (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:317:13) at C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:335:12) at next (C:\users\gaffer\desktop\gaffercart\node_modules\express\lib\router\index.js:275:10) at C:\users\gaffer\desktop\gaffercart\node_modules\connect-flash\lib\flash.js:21:5

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

// var mysql=require('mysql');
// var Sequelize=require('sequelize');
// var DataTypes=Sequelize.DataTypes;
// var sequelize = exports.sequelize = module.parent.exports.sequelize;
// let sequelize=new Sequelize();

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

/*
* GET PAGES INDEX its correct but commiting it
*/

 router.get('/',function(req,res){
 //  Page.find({}).sort({sorting: 1}).exec(function(err,pages){
    res.render('admin/pages',{ 
      //pages:pages
    });
   });
 //});
//  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){
  //  console.log("alsdjlajsi")

  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 there are Errors then define it on the same page
  if(errors){
    res.render('admin/add_page',{
      errors:errors,
      title:title,
      slug:slug,
      content:content
    });
  }
  /*
  * Have use Pages
  */
      else{

        models.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:100
            });
            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 mine Page models

'use strict';
// var Sequelize=require('sequelize');

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;
};

2 Answers 2

7

You've defined your Page model as 'page' (lowercase):

var page = sequelize.define('page', {...})

But are using it as:

 models.Page.findOne(...) // uppercase

Either use models.page or change your model definition to be uppercase:

var page = sequelize.define('Page', {...}) // uppercase

Also change this:

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

to this:

const Page = require('../models').Page // or .page if you keep your definition lowercase

findOne does not take a function callback, and you must use where to search by the slug property. Use the following:

else { 
  models.Page.findOne({ 
    where: { slug }  // destructuring
  })
  .then((page) => {
    // work with the page instance
  })
  .catch((err) => {
    // error case
  })

}

same with page.save():

page.save()
  .then(() => {
    req.flash('success','Page Added Successfully!!');
    res.redirect('/admin/pages');
  })
  .catch((err) => {
    // handle error
  })
Sign up to request clarification or add additional context in comments.

6 Comments

I am getting this error now friend you are right above it take my one week to find this (sequelize) Warning: Model attributes (slug) passed into finder method options of model Page, but the options.where object is empty. Did you forget to use options.where? Executing (default): SELECT id, title, slug, content, sorting, createdAt, updatedAt FROM Pages AS Page LIMIT 1; Can you please also help me with this
models.Page.findOne({slug: slug}, function (err, page) { if (page) {
use where and findOne does not take a function callback, it returns a Promise so you have to use then or async/await. I updated my answer. See the example there.
can you tell me how to that in my case
|
0
you need to check following things your_model.js file

 1. check name of model (wallet_transaction or wallet_transactions)
 2. if model (table ) name create with 's then need to set in model also 's 
like wallet_transactions so please check one more time 
 3. then in controller you import model then check name here it's proper or not 
 4. then set break point in your query and check what's the  error (cannot read properties of undefined (reading 'findone')) or not

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.