如何访问以下用户模型(基于猫鼬)的名字和姓氏?
How to to access first and last name for the following user model (based on mongoose)?
我正在查看一些带有用户架构的代码,类似于以下内容。
var UserSchema = new mongoose.Schema(
{
email: {
type: String,
lowercase: true,
unique: true,
required: true
},
password: {
type: String,
required: true
},
profile: {
firstName: {
type: String
},
lastName: {
type: String
}
}
}
);
现在,据我所知,顶级属性是电子邮件、密码和个人资料.... firstName
和 lastName
应该只能从 [=14= 中访问].但是,正在使用类似以下内容访问详细信息。
exports.register = function(req, res, next) {
// Check for registration errors
const email = req.body.email;
const password = req.body.password;
const firstName = req.body.firstName;
const lastName = req.body.lastName;
// Return error if no email provided
if (!email) {
return res.status(422).send({ error: 'You must enter an email address.'});
}
// Return error if no password provided
if (!password) {
return res.status(422).send({ error: 'You must enter a password.' });
}
// Return error if full name not provided
if (!firstName || !lastName) {
return res.status(422).send({ error: 'You must enter your full name.'});
}
...
我似乎不明白为什么 firstName
和 lastName
被直接访问 req.body.firstName
而不是 req.body.profile.firstName
。似乎也没有任何虚拟属性。所以这是怎么回事!?
正如@DanielKhan 所指出的,在上述评论中,mongoose 仅用于对数据建模。但是,此时,它与直接从客户端传入的数据无关。因此,所有字段,包括电子邮件、密码、名字和姓氏都将在同一级别检索...使用 req.body
.
req.body
由 body-parser 添加,这与您的猫鼬模型无关。您将获得从前端(客户端)发送的 req.body
中的数据。除了这个问题,我还建议您使用以下可能对您有帮助的格式
您可能喜欢为 sub-document
使用架构
var profileSchema = new Schema({
firstName: String,
lastName: String
});
var UserSchema = new mongoose.Schema({
email: {
type: String,
lowercase: true,
unique: true,
required: true
},
password: {
type: String,
required: true
},
profile: profileSchema
});
并且可以使用
exports.register = function(req, res, next) {
if(!req.body)
return res.status(500).send({ error: 'Unable to parse data'});
// Check for registration errors
const userData = {
email: req.body.email,
password: req.body.password,
profile: {
firstName: req.body.firstName,
lastName: req.body.lastName
}
}
// Return error if no email provided
if (!userData.email) {
return res.status(422).send({ error: 'You must enter an email address.'});
}
// Return error if no password provided
if (!userData.password) {
return res.status(422).send({ error: 'You must enter a password.' });
}
// Return error if full name not provided
if (!userData.profile.firstName || !userData.profile.lastName) {
return res.status(422).send({ error: 'You must enter your full name.'});
}
var newUser = new User(userData);// you must import user schema before using User
在快速应用程序中,请求参数作为路由的第一个参数传入 (req, res, next)
。发布的示例代码显示了对名为 /register 的路由的 POST 请求的结果。
此数据与随问题发布的模型无关。
为了能够使用模型,需要将数据存储到新的 Mongoose 对象中。
所以在路线中会写:
exports.register = function(req, res, next) {
const User = new User();
User.profile.firstName = req.body.firstName;
// ... and so on
User.save((err, savedUser) => {
if(err) return next(err);
// end request
});
}
请注意,在处理用户提供的变量时,建议进行某种健全性检查。在我上面的示例中使用它可能会使攻击者能够在数据库中存储任意长度的字符串,这很可能是不需要的。
我正在查看一些带有用户架构的代码,类似于以下内容。
var UserSchema = new mongoose.Schema(
{
email: {
type: String,
lowercase: true,
unique: true,
required: true
},
password: {
type: String,
required: true
},
profile: {
firstName: {
type: String
},
lastName: {
type: String
}
}
}
);
现在,据我所知,顶级属性是电子邮件、密码和个人资料.... firstName
和 lastName
应该只能从 [=14= 中访问].但是,正在使用类似以下内容访问详细信息。
exports.register = function(req, res, next) {
// Check for registration errors
const email = req.body.email;
const password = req.body.password;
const firstName = req.body.firstName;
const lastName = req.body.lastName;
// Return error if no email provided
if (!email) {
return res.status(422).send({ error: 'You must enter an email address.'});
}
// Return error if no password provided
if (!password) {
return res.status(422).send({ error: 'You must enter a password.' });
}
// Return error if full name not provided
if (!firstName || !lastName) {
return res.status(422).send({ error: 'You must enter your full name.'});
}
...
我似乎不明白为什么 firstName
和 lastName
被直接访问 req.body.firstName
而不是 req.body.profile.firstName
。似乎也没有任何虚拟属性。所以这是怎么回事!?
正如@DanielKhan 所指出的,在上述评论中,mongoose 仅用于对数据建模。但是,此时,它与直接从客户端传入的数据无关。因此,所有字段,包括电子邮件、密码、名字和姓氏都将在同一级别检索...使用 req.body
.
req.body
由 body-parser 添加,这与您的猫鼬模型无关。您将获得从前端(客户端)发送的 req.body
中的数据。除了这个问题,我还建议您使用以下可能对您有帮助的格式
您可能喜欢为 sub-document
使用架构var profileSchema = new Schema({
firstName: String,
lastName: String
});
var UserSchema = new mongoose.Schema({
email: {
type: String,
lowercase: true,
unique: true,
required: true
},
password: {
type: String,
required: true
},
profile: profileSchema
});
并且可以使用
exports.register = function(req, res, next) {
if(!req.body)
return res.status(500).send({ error: 'Unable to parse data'});
// Check for registration errors
const userData = {
email: req.body.email,
password: req.body.password,
profile: {
firstName: req.body.firstName,
lastName: req.body.lastName
}
}
// Return error if no email provided
if (!userData.email) {
return res.status(422).send({ error: 'You must enter an email address.'});
}
// Return error if no password provided
if (!userData.password) {
return res.status(422).send({ error: 'You must enter a password.' });
}
// Return error if full name not provided
if (!userData.profile.firstName || !userData.profile.lastName) {
return res.status(422).send({ error: 'You must enter your full name.'});
}
var newUser = new User(userData);// you must import user schema before using User
在快速应用程序中,请求参数作为路由的第一个参数传入 (req, res, next)
。发布的示例代码显示了对名为 /register 的路由的 POST 请求的结果。
此数据与随问题发布的模型无关。
为了能够使用模型,需要将数据存储到新的 Mongoose 对象中。 所以在路线中会写:
exports.register = function(req, res, next) {
const User = new User();
User.profile.firstName = req.body.firstName;
// ... and so on
User.save((err, savedUser) => {
if(err) return next(err);
// end request
});
}
请注意,在处理用户提供的变量时,建议进行某种健全性检查。在我上面的示例中使用它可能会使攻击者能够在数据库中存储任意长度的字符串,这很可能是不需要的。