无服务器API离线测试接收验证错误时,发送post请求数据到MongoDB通过邮差



我正在通过邮差对我的无服务器api进行一些后端测试,我发送的数据导致Users validation failed: email: Path email is required., name: Path name is required., password: Path password is required.

用户模型

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();
});

用户处理

/* 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,
});
});
};

我通过Postman发送的数据

curl --location --request POST 'http://localhost:3000/prod/users' 
--header 'Content-type: application/json' 
--data-raw '{"name": "hello", "password": "pass", "email": "asdf@asf.com"}'

预期结果

发送数据应该导致创建新的用户和api密钥,并将它们存储在MongoDB中。我没有使用快速服务器,所以我假设是路由器。发布信息,数据没有被正确路由。如果是这样的话,我需要改变或实现什么才能通过邮差创建用户?

我必须将数据作为x-www-form-urlencoded中的键值对传递

相关内容

  • 没有找到相关文章

最新更新