我正在尝试在node.js应用程序中执行post请求,但不断收到一个我无法理解的错误。
{
"message": "\"birthYear\" is not allowed",
"path": [
"birthYear"
],
"type": "object.allowUnknown",
"context": {
"child": "birthYear",
"value": "1992",
"key": "birthYear",
"label": "birthYear"
}
}
我试图为创建的每个用户添加一个“生日”字段。首先,我将用户模型设置为:
const UserSchema = new Mongoose.Schema({
email: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true },
username: { type: String, required: true },
admin: { type: Boolean, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
birthYear: { type: String, required: true },
});
如果我尝试在没有生日字段的情况下进行post请求,我会在postman中收到一条消息,说这是必需的。
这是我的控制器的外观
public async create(request, h): Promise<Hapi.ServerResponse> {
try {
const payload: User = request.payload;
const exists: boolean = await UserRepository.doesExist(payload);
if (exists) {
throw new Error("A user already exists with that email.");
}
const user: User = await UserRepository.create(payload);
console.log(payload);
const token = Token.create(user);
user.password = undefined;
const response: Login = {
// eslint-disable-next-line @typescript-eslint/camelcase
auth_token: token,
user,
};
return h.response(response);
} catch (error) {
return h
.response({ status: "error", error: error.message })
.code(400)
.takeover();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!