gpt4 book ai didi

express - 无法读取 Node 和 Sequelize 中未定义的属性 'findOne'

转载 作者:行者123 更新时间:2023-12-03 22:17:50 30 4
gpt4 key购买 nike

这是我的 admin_pages.js 文件,我已完成迁移和模型,但出现此错误。

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;

这是我的页面模型

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

最佳答案

您已将 Page 模型定义为 'page'(小写):

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

但将其用作:

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

使用 models.page 或将模型定义更改为大写:

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

同时改变这个:

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

为此:

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

findOne 不接受函数回调,您必须使用 where 通过 slug 属性进行搜索。使用以下内容:

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

}

page.save() 相同:

page.save()
.then(() => {
req.flash('success','Page Added Successfully!!');
res.redirect('/admin/pages');
})
.catch((err) => {
// handle error
})

关于express - 无法读取 Node 和 Sequelize 中未定义的属性 'findOne',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49947876/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com