使用节点 js 填充响应中的 ObjectId 数组



我是 NodeJS 编程的新手,正在寻找有关如何在 json Object 中填充对象数组类型的响应属性的解决方案。

以下是我的服务的示例响应 -

{
"Roles": [
"5b7fd72537651320a03494f8",
"5b7fdc06e3fcb037d016e26a"
],
"_id": "5b8e530be2d4630fd4f4b34a",
"Username": "ctucker",
"FirstName": "Chris",
"LastName": "Tucker",
"Email": "ctucker@innovate.com",
"createdAt": "2018-09-04T09:40:27.091Z",
"updatedAt": "2018-09-04T09:40:27.091Z",
"__v": 0
}

我想用实际名称而不是 Id 填充角色数组属性中的 ObjectId。

我应该怎么做。

以下是我定义用户和角色架构的方式

用户模型

const userSchema = new mongoose.Schema({
Username: {
type: String,
required: true,
minlength: 5,
maxlength: 255,
unique: true
},
FirstName: {
type: String
},
LastName: {
type: String
},
Email: {
type: String,
required: true,
minlength: 5,
maxlength: 255
},
Password: {
type: String,
required: true,
minlength: 5,
maxlength: 1024
},
Roles: [{type: mongoose.Schema.Types.ObjectId, ref: Roles}],
Active: {type: Boolean, default: true},
SuperUser: {type: Boolean, default: false}
},{
timestamps: true
});

榜样

const rolesSchema = new mongoose.Schema({
RoleName: {
type: String,
required: true,
minlength: 5,
maxlength: 255,
unique: true
},
Description: {
type: String
},
Active: {type: Boolean, default: true}
}, {
timestamps: true
});

创建用户

router.post('/', [auth, admin], async (req, res) => {
const { error } = validate(req.body); 
if (error) return res.status(400).send(error.details[0].message);
let user = await User.findOne({ Username: req.body.Username });
if (user) return res.status(400).send('User with username: ', req.body.Username, 'already exists. Please try with any other username');
let roleInput = [];
if(req.body.Roles !== null) {
req.body.Roles.forEach( async (element) => {
objectId = mongoose.Types.ObjectId(element);
roleInput.push(objectId);
});
}
console.log('Role info ', roleInput);
user = new User(_.pick(req.body, ['Username', 'FirstName', 'LastName' ,'Email', 'Password', 'Active','SuperUser']));
console.log('User Input => ', user);
const salt = await bcrypt.genSalt(10);
user.Password = await bcrypt.hash(user.Password, salt);
roleInput.forEach( async (objectId) => {
user.Roles.push(objectId);
});  
console.log('User Input after role addition => ', user);
await user.save();
const token = user.generateAuthToken();
res.header('x-auth-token', token).send(_.pick(user, ['_id', 'FirstName', 'LastName' ,'Email', 'Roles']));
});

我想在响应中获取角色名称而不是对象 ID。 在这方面的任何帮助将不胜感激。

忘了提到我已经尝试使用填充方法,但我看到以下异常。

我如何使用填充方法 -

router.get('/', auth, (req, res, next) => {
var request_params = url.parse(req.url,true).query;
var searchQuery = (request_params.mode === 'active') ? {'Active': true} : {};
User.find(searchQuery)
.populate('Roles')
.select(['-Password', '-Active', '-SuperUser'])
.then((users) => {
res.send(users);
}, (error) => {
next(error);
});
});

例外-

<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<h1>Schema hasn't been registered for model &quot;[object Object]&quot;.
Use mongoose.model(name, schema)</h1>
<h2></h2>
<pre>MissingSchemaError: Schema hasn't been registered for model &quot;[object Object]&quot;.
Use mongoose.model(name, schema)
at new MissingSchemaError (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooseliberrormissingSchema.js:20:11)
at NativeConnection.Connection.model (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooselibconnection.js:791:11)
at getModelsMapForPopulate (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooselibmodel.js:4016:20)
at populate (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooselibmodel.js:3505:21)
at _populate (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooselibmodel.js:3475:5)
at utils.promiseOrCallback.cb (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooselibmodel.js:3448:5)
at Object.promiseOrCallback (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooselibutils.js:232:14)
at Function.Model.populate (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooselibmodel.js:3447:16)
at cb (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongooselibquery.js:1678:17)
at result (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongoosenode_modulesmongodblibutils.js:414:17)
at executeCallback (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongoosenode_modulesmongodblibutils.js:406:9)
at handleCallback (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongoosenode_modulesmongodblibutils.js:128:55)
at cursor.close (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongoosenode_modulesmongodbliboperationscursor_ops.js:211:62)
at handleCallback (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongoosenode_modulesmongodblibutils.js:128:55)
at completeClose (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongoosenode_modulesmongodblibcursor.js:887:14)
at Cursor.close (C:Vijay-DocsPersonalvuejs-wsAgileCenteragilecenterservicesnode_modulesmongoosenode_modulesmongodblibcursor.js:906:10)</pre>
</body>
</html>

理解了问题。

在用户架构定义中,我还必须定义角色架构。

const rolesSchema = new mongoose.Schema({
RoleName: {
type: String,
required: true,
minlength: 5,
maxlength: 255,
unique: true
},
Description: {
type: String
},
Active: {type: Boolean, default: true}
}, {
timestamps: true
});
const Roles = mongoose.model('Roles', rolesSchema);
const userSchema = new mongoose.Schema({
Username: {
type: String,
required: true,
minlength: 5,
maxlength: 255,
unique: true
},
FirstName: {
type: String
},
LastName: {
type: String
},
Email: {
type: String,
required: true,
minlength: 5,
maxlength: 255
},
Password: {
type: String,
required: true,
minlength: 5,
maxlength: 1024
},
Roles: [{type: mongoose.Schema.Types.ObjectId, ref: 'Roles'}],
Active: {type: Boolean, default: true},
SuperUser: {type: Boolean, default: false}
},{
timestamps: true
});

在用户架构中添加角色架构定义后,邮递员的响应会在响应中填充角色名称。

{
"Roles": [
{
"RoleName": "AC-User"
},
{
"RoleName": "AC-PMLead"
}
],
"_id": "5b8e48e2f3372a098c6e5346",
"Username": "mcmillan",
"FirstName": "McMillan",
"LastName": "McMillan",
"Email": "mcmillan@innovate.com",
"createdAt": "2018-09-04T08:57:07.029Z",
"updatedAt": "2018-09-04T08:57:07.029Z",
"__v": 0
}

使用猫鼬populate()函数很简单

const user = await User.findOne().populate('Roles');
// output => user.Roles // will be array of mongo documents

干杯

您可以使用猫鼬的填充来填充RoleName而不是ObjectId引用

Mongoose 有一个更强大的替代方案,称为populate((,它允许您引用其他集合中的文档。

'use strict';
let user = await User.findOne({
Username: req.body.Username
}).populate('Roles');
if (user && user.Roles.length > 0) {
for (let role of user.Roles) {
console.log(role.RoleName);// logs `RoleName`
}
}

相关内容

  • 没有找到相关文章

最新更新