models / users.js
const mongoose = require('mongoose');
const Joi = require('joi');
const User = mongoose.model('user', new mongoose.Schema({
name : {
type : String,
required:true,
minlength : 5,
maxlength : 50
},
email : {
type:String,
required:true,
unique:true,
minlength:5,
maxlength:255
},
password : {
type:String,
required:true,
minlength:8,
maxlength:1024
}
}));
function validateUser(user){
const schema = {
name : Joi.string().min(5).max(50).required(),
email : Joi.string().min(5).max(255).required().email(),
password:Joi.string().min(5).max(255).required()
};
return Joi.validate(schema,user);
};
module.exports.User = User;
module.exports.validate=validateUser;
routes / users.js
const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();
const bodyparser = require('body-parser');
const bodyEncodedParser = bodyparser.json();
const {User,validate}=require('../models/users');
router.post('/',bodyEncodedParser,async(req,resp)=>{
const {error} = validate(req.body);
if (error) return resp.status(400).send(error.details[0].message);
//to check if user already not registered
let user = await User.findOne({email : req.body.email});
if(user) return resp.status(400).send("user already registered");
user = new User({
name :req.body.name,
email :req.body.email,
password :req.body.password
});
await user.save();
resp.send(user);
});
module.exports =router;
嗨,当我从我的app.js中运行时。程序运行正常,数据库也已连接。当我使用邮递员发送带有有效名称电子邮件和密码的POST请求时,出现错误。 我想在发送rqst后对我的http主体进行json格式的验证来实现这一点。如果不存在电子邮件。我在http正文中提供的数据被添加到数据库中。 我不明白为什么即使我发送了一个有效的json对象,数据剂量也被保存了。 每次我发送使用后它给我以下错误。 我在使用await方法时还没有完成错误处理。我知道如果我发送错误的json对象,它将给我错误。但是当使用邮递员通过POST发送正确的json对象时,为什么会给我错误
nhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at stringify (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\response.js:1119:12)
at ServerResponse.json (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\response.js:260:14)
at ServerResponse.send (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\response.js:158:21)
at router.post (C:\Users\AMIT SINGH\Desktop\vidly\routes\users.js:12:40)
at Layer.handle [as handle_request] (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\route.js:137:13)
at jsonParser (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\body-parser\lib\types\json.js:101:7)
at Layer.handle [as handle_request] (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\AMIT SINGH\Desktop\vidly\node_modules\express\lib\router\index.js:174:3)(node:5452) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5452) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.