我有多个参数的猫鼬的模式,我想是猫鼬返回一条消息,如果一个参数是未知的。
例如,如果我有名字和年龄,如果不是必需的年龄,但用户写agge,我想返回警告用户,但我没有发现,在猫鼬文档。
谢谢 !
如果你在后台写代码,不写你没有所需要的。
例如,你的模式是这样的:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {
type: String,
required: true
},
age:String //not required
和你的休息API
app.post('/user', (req,res) => {
const newData = new UserSchema({
name : req.body.name
//although user post age, don't create age field, ignore that
})
newData.save().then().catch(err => console.log(err))
})
另一种方法,创建验证,如果用户输入年龄,发送响应(“不需要”)。
我希望这条线索可以帮助你。谢谢