While conducting backend testing of my serverless API using Postman, the data I'm sending is triggering the error message
Users validation failed: email: Path email is required., name: Path name is required., password: Path password is required.
User Model
const userSchema = new mongoose.Schema(
{
email: {
type: String,
trim: true,
required: true,
unique: true,
validate(value){
if(!validator.isEmail(value)){
throw new Error ("Please enter correct email");
}
}
},
name: {
type: String,
trim: true,
required: true,
},
password: {
type: String,
required: true,
},
salt: String,
role: {
type: String,
default: "Normal",
},
created: {
type: "Date",
default: Date.now,
},
subscription: {
type: String,
default: "dev",
},
token: {
type: String,
default: "free",
},
{ collection: "Users" }
);
userSchema.post("save", function (_doc, next) {
_doc.password = undefined;
return next();
});
User Handler
/* Create User*/
module.exports.create = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
Database.connectToDatabase()
.then(() => {
let body = querystring.decode(event.body);
console.log(event.body)
const randomKey = uuidv4();
let newUser = new User({
name: body.name,
email: body.email,
password: body.password,
apiKey: randomKey.replace(/-/g, ""),
});
//console.log("TESTING")
newUser.save(function (err, user) {
if (err) {
callback(null, {
statusCode: err.statusCode || 500,
headers: { "Content-Type": "text/plain" },
body: err.message,
});
} else {
callback(null, {
statusCode: 200,
body: JSON.stringify(user),
});
}
});
})
.catch((err) => {
callback(null, {
statusCode: err.statusCode || 500,
headers: { "Content-Type": "text/plain" },
body: err.message,
});
});
};
Data Being Sent through Postman
curl --location --request POST 'http://localhost:3000/prod/users' \
--header 'Content-type: application/json' \
--data-raw '{"name": "hello", "password": "pass", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e584968183a5849683cb868a88">[email protected]</a>"}'
Expected Outcome
The intent behind sending this data should result in the creation of a new user and API key, storing them in MongoDB. As I'm not utilizing an express server for this process, it's possible that the data isn't being routed correctly. If this is indeed the case, what adjustments or additions do I need to make in order to successfully create a user via Postman?