我得到了一个验证错误需要找到一个路径所需的,这个上传到数据库的确切方法是为我工作完美的网站上的另一种形式
//这是html(ejs)
<div className="sign-in-area">
<form action="/signin" method="POST" enctype="multipart/form-data">
<div>
<label for="userName">UserName:</label>
<input type="text" id="userName" name="username" required/>
</div>
<div>
<label for="email">Enter Email</label>
<input type="email" id="email" name="email" required/>
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" name="password" required/>
</div>
<button>Submit</button>
</form>
</div>
那么这就是mongoose用户模式
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const blogSchemaUser = new Schema({
username: {
type: String,
required: true,
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
}, { timestamps: true });
const User = mongoose.model('user',blogSchemaUser);
module.exports = User;
到mongodb的服务器连接
const app = express();
// middleware & static files
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
// connect to mongodb & listen for requests
const dbURL =
"mongodb+srv://myusernameandpassword@samstam.hte26or.mongodb.net/cookingSite";
mongoose.connect(dbURL, { useNewUrlParser: true,
useUnifiedTopology: true })
然后(结果=〉应用程序监听(8080)).捕获(错误=〉控制台日志(错误));
以及登录的路径
const express = require('express');
const router = express.Router();
const User = require('../models/user');
router.get('/signin', (req, res) => {
res.render('signin', { title: 'Create a new blog'
})
})
router.post('/signin', (req, res) => {
const user = new User({
username: req.body.username,
email: req.body.email,
passcode: req.body.password
});
user.save()
.then(result => {
res.redirect('/');
console.log(user)
})
.catch(err => {
console.log(err);
});
});
错误
Error: user validation failed: username: Path username
is required., email: Path email
is required., passcode: Path passcode
is required. at ValidationError.inspect (/Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/error/validation.js:50:26) at formatValue (node:internal/util/inspect:782:19) at inspect (node:internal/util/inspect:347:10) at formatWithOptionsInternal (node:internal/util/inspect:2167:40) at formatWithOptions (node:internal/util/inspect:2029:10) at console.value (node:internal/console/constructor:324:14) at console.log (node:internal/console/constructor:360:61) at /Users/samraitan/Documents/GitHub/website-food/routes/userRoutes.js:24:17 at processTicksAndRejections (node:internal/process/task_queues:96:5) { errors: { username: ValidatorError: Path username
is required. at validate (/Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/schematype.js:1346:13) at SchemaString.SchemaType.doValidate (/Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/schematype.js:1330:7) at /Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/document.js:2877:18 at processTicksAndRejections (node:internal/process/task_queues:78:11) { properties: [Object], kind: 'required', path: 'username', value: undefined, reason: undefined, [Symbol(mongoose:validatorError)]: true }, email: ValidatorError: Path email
is required. at validate (/Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/schematype.js:1346:13) at SchemaString.SchemaType.doValidate (/Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/schematype.js:1330:7) at /Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/document.js:2877:18 at processTicksAndRejections (node:internal/process/task_queues:78:11) { properties: [Object], kind: 'required', path: 'email', value: undefined, reason: undefined, [Symbol(mongoose:validatorError)]: true }, passcode: ValidatorError: Path passcode
is required. at validate (/Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/schematype.js:1346:13) at SchemaString.SchemaType.doValidate (/Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/schematype.js:1330:7) at /Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/document.js:2877:18 at processTicksAndRejections (node:internal/process/task_queues:78:11) { properties: [Object], kind: 'required', path: 'passcode', value: undefined, reason: undefined, [Symbol(mongoose:validatorError)]: true } }, _message: 'user validation failed' } ^C samraitan@SRs-MacBook-Pro website-food % node app2 /Users/samraitan/Documents/GitHub/website-food/app2.js:71 router.post('/postss', (req, res) => { ^
参考错误:路由器未在对象.(/Users/samraitan/Documents/GitHub/website-food/app2.js:71:1)中定义在模块._compile(节点:内部/模块/cjs/loader:1103:14)中定义在对象.模块._extensions.. js(节点:内部/模块/cjs/loader:1157:10)中定义在模块. load(节点:内部/模块/cjs/loader:981:32)中定义在函数.模块._load(节点:内部/模块/cjs/loader:822:12)中定义在函数. executeUserEntryPoint [as runMain](节点:内部/模块/运行_主:77:12)中定义在节点:内部/主/运行_模块:17:47中定义在函数.模块._load(节点:内部/模块/cjs/loader:822:12)中定义在函数.用户验证失败:image:需要路径image
。,正文:路径body
是必需的。,标题:路径title
是必需的。在验证时错误。检查(/Users/samraitan/Documents/GitHub/website-food/node_modules/mongoose/lib/error/validation. js:50:26)在格式时值(节点:内部/util/检查:782:19)在检查(节点:内部/util/检查:347:10)在格式时选项内部(节点:内部/util/检查:2167:40)在格式时选项(节点:内部/util/检查:2029:10)在控制台时。值(节点:内部/控制台/构造函数:324:14)在console.log(节点:内部/控制台/构造函数:360:61)在/Users/samraitan/Documents/GitHub/website-food/routes/用户时路由. js:24:17在处理时间点和拒绝(节点:内部/进程/任务队列:{图像:验证程序错误:路径image
是必需的。在验证(/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/架构类型. js:1346:13)在架构字符串。架构类型. doValidate(/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/架构类型. js:1330:7)在/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/文档. js:2877:18在处理滴答和拒绝(节点:内部/进程/任务队列:78:11){属性:【对象】,种类:"必需",路径:"图像",值:未定义,原因:未定义,[符号(mongoose:验证器错误)]:true},正文:验证器错误:路径body
是必需的。在验证(/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/架构类型. js:1346:13)在架构字符串。架构类型. doValidate(/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/架构类型. js:1330:7)在/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/文档. js:2877:18在处理滴答和拒绝(节点:内部/进程/任务队列:78:11){属性:【对象】,种类:"必需",路径:"正文",值:未定义,原因:未定义,[符号(mongoose:验证器错误)]:真},标题:验证器错误:路径title
是必需的。在验证(/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/架构类型. js:1346:13)在架构字符串。架构类型. doValidate(/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/架构类型. js:1330:7)在/用户/samraitan/文档/GitHub/网站-食物/节点_模块/ Mongoose /库/文档. js:2877:18在处理滴答和拒绝(节点:内部/进程/任务队列:78:11){属性:【对象】,种类:"必需",路径:"标题",值:未定义,原因:未定义,[符号(mongoose:验证器错误)]:真}},消息:'用户验证失败'}
1条答案
按热度按时间zzwlnbp81#
尝试console.log您的输入值一样(username.value),我敢肯定的错误应该是在主脚本文件.