Javascript NodeJS TypeError:Post不是构造函数
我有一个公式,我想在提交后回复消息。 正在使用节点v13.3.0, “快车”:“^4.17.1”, “快速车把”:“^3.1.0”, “快速会话”:“^1.17.0”, “flash”:“^1.1.0”, “把手”:“^4.7.3”, “猫鼬”:“^5.9.2” Post.jsJavascript NodeJS TypeError:Post不是构造函数,javascript,node.js,mongodb,express,typeerror,Javascript,Node.js,Mongodb,Express,Typeerror,我有一个公式,我想在提交后回复消息。 正在使用节点v13.3.0, “快车”:“^4.17.1”, “快速车把”:“^3.1.0”, “快速会话”:“^1.17.0”, “flash”:“^1.1.0”, “把手”:“^4.7.3”, “猫鼬”:“^5.9.2” Post.js const mongoose = require('mongoose'); const Schema = mongoose.Schema; // get props const PostSchema = ne
const
mongoose = require('mongoose');
const Schema = mongoose.Schema; // get props
const PostSchema = new Schema({
// define props --> required for a post
surname: {
type: String,
required: true
},
name: {
type: String,
required: true
},
biography: {
type: String,
required: true,
},
profilpicture: {
type: Object,
required: true,
}
});
module.exports = mongoose.model('Post', PostSchema);
Controller.js
const Post = require('../models/postModel').Post;
module.exports = {
submitPosts: (req, res) => {
// res.render('admin/posts/submit');
// Check the attributs from create.handlebars for success or error message
const newPost = new Post( {
surname: req.body.surname,
name: req.body.name,
biography: req.body.biography,
profilpicture: req.body.profilpicture
});
// Safe new posts
newPost.save().then(post => {
console.log(post);
flash('success-message', 'Yay its fine')
res.redirect('/admin');
});
},
}
我用于每个输入名称的公式文件=“attribute”
如果我删除帖子(Controller.js),它会抛出另一个错误:
(节点:2296)未处理的PromiserEjectionWarning:ValidationError:验证后失败:姓氏:Path姓氏
是必需的,名称:Pathname
是必需的,传记:Path传记
是必需的,profilpicture:Pathprofilpicture
是必需的。
当您执行以下操作时,如果站点记录了未定义的
,,则不会停止加载:
console.log(req.body.surname);
这意味着您没有收到预期的POST数据
确保您正在使用中间件:
const express = require("express");
const app = express();
const port = 3000;
app.use(express.urlencoded({ extended: true })); // <--------
/* ... */
const express=require(“express”);
常量app=express();
常数端口=3000;
app.use(express.urlencoded({extended:true}));//将require('../models/postModel')。Post
替换为require('../models/postModel')
,如果这是它导出的唯一内容准备好了,但它会抛出一条新的错误消息:unhandledPromisejectionWarning:ValidationError:Post validation失败:姓氏:Path姓氏
。,名称:路径name
。。所以onOk,我的错,在第一次读你的问题的时候没有意识到这一点。然而,这个错误比另一个好。所以,一定要删除该.Post
。现在已经解决了这个问题,在尝试创建newPost
之前,当您console.log(req.body.姓氏)
等时,是否看到了正确的数据?如果在console.log(req.body.姓氏)表示未定义之前和抛出“未经处理的拒绝承诺。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。(拒绝id:2)“我在我的app.js app.use(express.json());app.use(express.urlcoded({extended:true}))中使用它好的,你如何从前端发出POST请求?你能把它添加到你的问题中吗?如果表单是作为表单数据发送的话,上面应该会起作用,也许?