简体   繁体   English

如何修复POST不是NodeJS中的功能错误

[英]How to fix POST is not function error in NodeJS

I have the following code an am getting a POST is not function error . 我有以下代码正在获取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 Post模型是

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) 那将使您导入的number成为number (实际上是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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM