查找刚刚在路径中创建的用户的
我正在制作一个MERN应用程序的快速原型,我有一个后端问题:我有一个User模型和一个Category模型,当用户注册时,我需要用从用户导出的对象导出的一些基本信息来填充类别模型可以稍后编辑。我想将每个类别的ref分配给刚刚创建的帐户ID。问题是我不明白如何检索刚刚创建的用户ID。这是我的路线(是的没有重构,对不起):
// @route POST api/users/register
// @desc Register user
// @access Public
router.post('/register', (req, res)=>{
//Validate req.body
const {errors, isValid} = validateRegisterInput(req.body);
//Check validation
if(!isValid){
return res.status(400).json(errors);
}
User.findOne({ email: req.body.email })
.then(user => {
if(user){
errors.email = 'Email already exists';
return res.status(400).json(errors)
} else {
const avatar = gravatar.url(req.body.email, {
s: '200', //Size
r: 'pg', //Rating
d: 'mm' //Default
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
//Hash the password and save
bcrypt.genSalt(10, (err, salt)=>{
bcrypt.hash(newUser.password, salt, (err, hash)=>{
if(err) throw err;
newUser.password = hash;
newUser.save()
.then(user => res.json(user))
.catch(err => console.log(err))
})
});
//Fill user categories with default categories
defaultIcons.map((icon) => {
const newCategory = new Category ({
name: icon.name,
type: icon.type,
icon: icon.icon
})
newCategory.save();
});
}
})
});
这是类别架构:
//Create Schema
const CategorySchema = new Schema({
//Every account is associated with actual User schema by id
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
name: {
type: String,
required: true
},
type: {
type: String,
required: true
},
icon: {
type: String,
required: true
}
})
什么是最好的解决方案?为我所做的类别设置一个单独的模式或者我可以在User模式中实现一个对象数组字段更好吗?
回答如下:你做的部分
if(err) throw err;
newUser.password = hash;
newUser.save()
.then(user => res.json(user))
.catch(err => console.log(err))
你已经解决了诺言中的user
。你可以
const newCreatedUserID = user._id
获取刚刚创建的用户ID。
查找刚刚在路径中创建的用户的
我正在制作一个MERN应用程序的快速原型,我有一个后端问题:我有一个User模型和一个Category模型,当用户注册时,我需要用从用户导出的对象导出的一些基本信息来填充类别模型可以稍后编辑。我想将每个类别的ref分配给刚刚创建的帐户ID。问题是我不明白如何检索刚刚创建的用户ID。这是我的路线(是的没有重构,对不起):
// @route POST api/users/register
// @desc Register user
// @access Public
router.post('/register', (req, res)=>{
//Validate req.body
const {errors, isValid} = validateRegisterInput(req.body);
//Check validation
if(!isValid){
return res.status(400).json(errors);
}
User.findOne({ email: req.body.email })
.then(user => {
if(user){
errors.email = 'Email already exists';
return res.status(400).json(errors)
} else {
const avatar = gravatar.url(req.body.email, {
s: '200', //Size
r: 'pg', //Rating
d: 'mm' //Default
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
//Hash the password and save
bcrypt.genSalt(10, (err, salt)=>{
bcrypt.hash(newUser.password, salt, (err, hash)=>{
if(err) throw err;
newUser.password = hash;
newUser.save()
.then(user => res.json(user))
.catch(err => console.log(err))
})
});
//Fill user categories with default categories
defaultIcons.map((icon) => {
const newCategory = new Category ({
name: icon.name,
type: icon.type,
icon: icon.icon
})
newCategory.save();
});
}
})
});
这是类别架构:
//Create Schema
const CategorySchema = new Schema({
//Every account is associated with actual User schema by id
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
name: {
type: String,
required: true
},
type: {
type: String,
required: true
},
icon: {
type: String,
required: true
}
})
什么是最好的解决方案?为我所做的类别设置一个单独的模式或者我可以在User模式中实现一个对象数组字段更好吗?
回答如下:你做的部分
if(err) throw err;
newUser.password = hash;
newUser.save()
.then(user => res.json(user))
.catch(err => console.log(err))
你已经解决了诺言中的user
。你可以
const newCreatedUserID = user._id
获取刚刚创建的用户ID。