我尝试在mongoose populate应用中使用keystone.js。从那以后,各种各样的麻烦随之而来。这是我的用户模型
'use strict';
const keystone = require('keystone');
const Types = keystone.Field.Types;
let Country = keystone.list('Country');
/**
* User Model
* ==========
*/
let User = new keystone.List('User', {history: true});
User.add({
name: {type: Types.Name, required: true, index: true},
email: {type: Types.Email, initial: true, required: true, unique: true, index: true},
password: {type: Types.Password, initial: true, required: false},
phone: {type: Types.Text, unique: true},
avatar: {type: Types.Text},
registrationEnd: {type: Types.Date},
registrationStart: {type: Types.Date},
utitle: {type: Types.Relationship, ref: 'Title'},
dob: {type: Types.Date, format: 'DD/MM/YYYY'},
permAddress: {
type: Types.Location,
state: {type: Types.Relationship, ref: 'State'},
country: {type: Types.Relationship, ref: 'Country'}
},
sor: {type: Types.Relationship, ref: 'State'},
lga: {type: Types.Relationship, ref: 'LGA'},
religion: {type: Types.Relationship, ref: 'Religion'},
marital: {type: Types.Relationship, ref: 'Marital'},
sex: {type: Types.Select, options: 'male, female'},
education: {type: Types.Relationship, ref: 'Education', dependsOn: {isClient: true}},
seenLawyer: {type: Boolean, default: false, dependsOn: {isClient: true}},
occupation: {type: Types.Relationship, ref: 'Occupation', dependsOn: {isClient: true}},
firm: {type: Types.Text, dependsOn: {isLawyer: true}},
lawyerSince: {type: Types.Date, dependsOn: {isLawyer: true}},
areas: {type: Types.Relationship, ref: 'LawArea', default: false, many: true, dependsOn: {isLawyer: true}},
}, 'Permissions', {
isAdmin: {type: Boolean, label: 'Can access Keystone', index: true, default: false},
isLawyer: {type: Boolean, index: true, default: false},
isClient: {type: Boolean, index: true, default: false},
});
User.schema.pre('save', function (next) {
let thisUser = this;
Country.model.findOne({name: 'Denmark'}).then((result) => {
thisUser.permAddress.country = result._id;
next();
});
});
User.schema.virtual('canAccessKeystone').get(function () {
return this.isAdmin;
});
User.schema.virtual('law').get(function () {
return this.isLawyer;
});
User.schema.virtual('client').get(function () {
return this.isClient;
});
User.schema.virtual('fullname').get(function () {
return this.name.first + ' ' + this.name.last;
});
// Return only a user's _id
User.statics = {
getUserId: function (email, cb) {
this.findOne({email: email}).select('_id').exec(cb);
}
};
User.defaultColumns = 'name, email, isLawyer, isAdmin';
User.register();
在模型中,utitle
,sor
,lga
和其他一些路径是关系,根据Keystone的说法,它们应引用有效的MongoDB ObjectID。现在,我尝试在查找时从该模型填充文档。这是路由器导出功能内的片段。
view.on('init', function (next) {
console.log(req.user.email);
keystone.list('User').model.findOne({email: req.user.email}).populate({
path: 'utitle',
select: 'name'
}).populate({path: 'sor', select: 'name'}).populate({path: 'lga', select: 'name'}).populate({
path: 'religion',
select: 'name'
}).populate({path: 'marital', select: 'name'}).populate({
path: 'education',
select: 'name'
}).populate({path: 'occupation', select: 'name'}).exec(function (err, results) {
if (err || !results.length) {
return next(err);
}
console.log(results); // Expect to find populated documents
locals.user = results; next(err);
});
});
locals.clientType = 'Free';
locals.profilelink = '/portal/profile';
// Render the view
if (link === undefined) {
view.render('portal/client', {layout: 'portal'});
} else {
view.render('client/' + link, {layout: 'portal'});
}
这是我的console.log
{ name: { first: 'Mike', last: 'Mikey' },
permAddress:
{ country: '5c08562d6ca7fa009b16d2ce',
postcode: '220282',
state: '5c08562d6ca7fa009b16d2d8',
street1: '22, Ajanlekoko Street, ',
suburb: 'Abule-Egba' },
seenLawyer: false,
areas: [],
isAdmin: false,
isLawyer: false,
isClient: true,
_id: 5c08562e6ca7fa009b16d310,
email: 'xxxxxx@xxx.xx',
password:
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
phone: '0703268xxxx',
__rev: 3,
__v: 0,
avatar: '',
dob: 2018-12-04T23:00:00.000Z,
registrationEnd: 2018-12-04T23:00:00.000Z,
registrationStart: 2018-12-04T23:00:00.000Z,
utitle: 5c08562e6ca7fa009b16d2ee,
education: 5c08562e6ca7fa009b16d2f9,
lga: 5c08562d6ca7fa009b16d2db,
marital: 5c08562e6ca7fa009b16d2e5,
occupation: 5c08562e6ca7fa009b16d309,
religion: 5c08562e6ca7fa009b16d2e0,
sex: 'female',
sor: 5c08562d6ca7fa009b16d2d8 }
与我完全不填充时相同。从管理界面可以看到,所有引用的模型都是完整的。
请原谅问题的粗略之处,并告诉我我在做错什么。