我尝试做的是通过执行简单的http get请求在我的某个网页上显示用户显示名称列表。
在我的users.server.controller.js文件中,我有这个功能:
exports.list = function(req, res) {
User.find().populate('displayName').exec(function(err, users) {
console.log(users);
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(users);
}
});
};
当我使用AngularJS控制器的$http.get()
请求调用此函数时,我得到400错误。
这是我的Mongoose用户架构
/**
* User Schema
*/
var UserSchema = new Schema({
firstName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your first name']
},
lastName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your last name']
},
organization: {
type: String,
trim: true,
default: '',
required: 'Please fill in an organization name'
},
position: {
type: String,
trim: true,
default: '',
required: 'Please fill in the title of your position'
},
displayName: {
type: String,
trim: true
},
email: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your email'],
match: [/.+\@.+\..+/, 'Please fill a valid email address']
},
username: {
type: String,
unique: 'testing error message',
required: 'Please fill in a username',
trim: true
},
password: {
type: String,
default: '',
validate: [validateLocalStrategyPassword, 'Password should be longer']
},
salt: {
type: String
},
provider: {
type: String,
required: 'Provider is required'
},
providerData: {},
additionalProvidersData: {},
roles: {
type: [{
type: String,
enum: ['user', 'admin']
}],
default: ['user']
},
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
/* For reset password */
resetPasswordToken: {
type: String
},
resetPasswordExpires: {
type: Date
}
});
答案 0 :(得分:1)
似乎你在populate和select之间混淆了。当您引用外部架构(如owner : { type: Schema.ObjectId, ref: 'User' }
)时,将使用populate。如果要显示所有者字段,则需要调用populate('owner')
来加载用户。您需要的是调用.select("displayName")
以便仅检索结果中的displayName。