需要在路径上验证失败,反应,节点和猫鼬
当我尝试从前端发送邮政请求时,我不是为什么从猫鼬后端验证失败的原因,但是如果我与Postman一起测试我的后端API请求,则可以正常工作。
反馈验证失败:接收者:路径
接收者
是必需的。 classcodes:路径classcodes
是必需的。 需要。内容:路径content
是必需的。
我的后端API
控制器
exports.createFeedback = async (req, res) => {
try {
console.log('data from frontend', req.body);
const feedback = await new FeedbackSchema({
receiver: req.body.receiver,
teacher: req.userId,
classCodes: req.body.classCodes,
level: req.body.level,
content: req.body.content,
send: req.body.send,
}).save();
return res.json({ feedback, message: 'Feedback Created' });
} catch (error) {
console.log(error.message);
res.json('Failed to create feedback!');
}
};
反馈模式
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const FeedbackSchema = new Schema({
receiver: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
teacher: {
type: Schema.Types.ObjectId,
ref: 'User',
},
level: {
type: Schema.Types.ObjectId,
ref: 'Level',
required: true,
},
classCodes: {
type: Schema.Types.ObjectId,
ref: 'ClassCode',
required: true,
},
content: {
type: String,
required: true,
maxlength: 300,
},
note: {
type: String,
maxlength: 300,
},
createdAt: {
type: Date,
default: Date.now(),
},
});
module.exports = {
FeedbackSchema: mongoose.model('Feedback', FeedbackSchema),
};
如果我从邮递员那里发送请求,则可以正常工作,而没有验证失败,但是如果我试图从前端发送请求,我会在上面的路径上收到验证。
请勿接触评分模式
注意:对于前端请求, 。如您所见,在将其发送到后端之前,我会记录所有数据,内容,级别,接收器和classCodes
,我也尝试console.log(req.body)
在后端,我也收到了这些数据,但仍显示道路上的验证失败。
后端日志
前端日志
Postman请求正常工作
I don't why this failed validation from the mongoose backend when I try to send the post request from the frontend, but if I test my backend API request with postman, it's working fine.
Feedback validation failed: receiver: Path
receiver
is required.,
classCodes: PathclassCodes
is required., level: Pathlevel
is
required., content: Pathcontent
is required.
controller for my backend API
exports.createFeedback = async (req, res) => {
try {
console.log('data from frontend', req.body);
const feedback = await new FeedbackSchema({
receiver: req.body.receiver,
teacher: req.userId,
classCodes: req.body.classCodes,
level: req.body.level,
content: req.body.content,
send: req.body.send,
}).save();
return res.json({ feedback, message: 'Feedback Created' });
} catch (error) {
console.log(error.message);
res.json('Failed to create feedback!');
}
};
Feedback Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const FeedbackSchema = new Schema({
receiver: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
teacher: {
type: Schema.Types.ObjectId,
ref: 'User',
},
level: {
type: Schema.Types.ObjectId,
ref: 'Level',
required: true,
},
classCodes: {
type: Schema.Types.ObjectId,
ref: 'ClassCode',
required: true,
},
content: {
type: String,
required: true,
maxlength: 300,
},
note: {
type: String,
maxlength: 300,
},
createdAt: {
type: Date,
default: Date.now(),
},
});
module.exports = {
FeedbackSchema: mongoose.model('Feedback', FeedbackSchema),
};
if I send a request from the postman is works fine without validation failed, but if I tried to send a request from the frontend, I receive a validation failed at Path above.
Note: don't border about the ratings schema
For frontend request. As you can see I log all the data before send it to the backend, content, level, receiver and classCodes
and I also try to console.log(req.body)
in the backend as well and I receive those data as well but still it show validation failed at Path is required.
Backend log
Frontend log
Postman request worked fine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在发送嵌套在
values
属性中的数据,请尝试更改您的代码:You are sending the data nested in a
values
property, try to change your code like this: