【发布时间】:2020-06-12 17:07:14
【问题描述】:
我有一个公式,我想在提交后收到回复消息。 使用 Node v13.3.0, “快递”:“^4.17.1”, "express-handlebars": "^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 = 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');
});
},
}
我用于每个输入的公式文件 name="attribut" 如果我删除 Post (Controller.js),它会引发另一个错误:
(node:2296) UnhandledPromiseRejectionWarning: ValidationError: Post validation failed: surname: Path surname is required., name: Path name is required., biography: Path biography is required., profilepicture : 路径 profilpicture 是必需的。
并且网站不会停止加载
【问题讨论】:
-
将
require('../models/postModel').Post替换为require('../models/postModel'),如果这是它唯一导出的内容 -
已经尝试过了,但它会抛出一个新的错误消息:UnhandledPromiseRejectionWarning: ValidationError: Post validation failed: surname: Path
surnameis required., name: Pathname.. 等等跨度> -
好吧,我的错,第一次阅读你的问题时没有弄清楚。但是,这个错误比另一个要好。所以,请删除
.Post。既然这已经不碍事了,那么在尝试创建newPost之前,您在console.log(req.body.surname)等时看到正确的数据了吗? -
如果我 console.log(req.body.surname) 在它说未定义之前和之后它抛出“未处理的承诺拒绝。这个错误源于在没有 catch 块的异步函数内部抛出,或者通过拒绝未使用 .catch() 处理的承诺。(rejection id: 2)"
标签: javascript node.js mongodb express typeerror