///api调用从axiosReact我已经创建了axios示例
export const register = (correctInput) => {
console.log(correctInput);
axios
.post("/api/auth/register", correctInput)
.then((res) => console.log(res.data))
.catch((err) => console.log(err));
};
///correctInput在这里我得到了发布数据所需的一切
Object { username: "test", mobileNo: "0000000000", email: "test@gmail.com", password: "test" }
apicall.js:5
///用户模式
const UserSchema = new mongoose.Schema(
{
username: { type: String, required: true, unique: true },
name: { type: String },
email: { type: String, required: true },
mobileNo: { type: Number, required: true, unique: true },
password: { type: String, required: true },
address: [
{
locality: { type: Number, unique: true },
pincode: { type: Number, unique: true },
city: { type: String, unique: true },
state: { type: String, unique: true },
country: {
type: String,
unique: true,
},
desc: {
type: String,
required: true,
},
},
],
isAdmin: {
type: Boolean,
default: false,
},
},
{ timestamps: true }
);
///从我的后端注册代码
router.post("/register", async (req, res) => {
const newUser = new User({
username: req.body.username,
email: req.body.email,
mobileNo: req.body.mobileNo,
password: CryptoJS.AES.encrypt(
req.body.password,
process.env.SEC_PASS
).toString(),
});
try {
const savedUser = await newUser.save();
res.status(201).json(savedUser);
} catch (err) {
console.log(err);
}
});
///这是一个错误,我给出了所有需要的信息,但它向我发送了验证错误
Error: User validation failed: username: Path `username` is required., email: Path `email` is required., mobileNo: Path `mobileNo` is required.
at ValidationError.inspect (F:\PICKO\PICOMERN\picko-backend\node_modules\mongoose\lib\error\validation.js:48:26)
1条答案
按热度按时间voase2hg1#
从外观上看,您的代码是正确的。这可能是解析JSON数据时出现的问题。
在主文件(后端)中,您必须执行
app.use(express.json());
,或者您可以选择使用app.use(express.json());