I have the following code an am getting a POST is not function error
. How can I fix this?
const express = require('express');
const router = express.Router();
const Post = require('../../models/Post');
router.all('/*',(req, res, next)=>{
req.app.locals.layout='admin';
next();
});
router.get('/',(req, res)=>{
res.send('It Works');
});
router.get('/create',(req, res)=>{
res.render('admin/posts/create');
});`// router.post('/create',(req, res)=>{
// res.send('worked');
// });
router.post('/create', (req, res)=>{
let allowComments=true;
if(req.body.allowComments){
allowComments=true;
}else{
allowComments=false;
}
Post({
title: req.body.title,
status: req.body.status,
allowComments:allowComments,
body: req.body.body
});
newPost.save().then(savedPost=>{
res.redirect('/admin/posts');
}).catch(error=>{
console.log('could not post');
});
// console.log(req.body);
});
module.exports=router;
And Post
model is
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PostSchema = new Schema({ // user:{ // // },
title: {
type: String,
required: true
},
status: {
type: String,
default: 'public'
},
allowComments: {
type: Boolean,
require: require
},
body: {
type: String,
require: true
}
});
module.exports = -mongoose.model('posts', PostSchema);
You have - sign here
module.exports = -mongoose.model('posts', PostSchema);
That would make your import a number
(actually NaN)
const Post = require('../../models/Post');
And a number is not a function.
const module = {} function foo(){ } module.exports = foo console.log(module) module.exports = -foo console.log(module)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.