我正在构建一个MEANjs应用程序,我有两个模式:用户和声明。我希望能够从索赔中引用用户信息。
现在,我可以使用表达式{{vm.claim.user.displayName}}成功访问视图中的显示名称。如何访问嵌入式用户架构的其他属性?
例如,我希望能够引用用户的firstName和lastName。 {{vm.claim.user.firstName}}之类的内容在我的视图中不会产生任何结果。
user.server.model.js
/**
* 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']
},
displayName: {
type: String,
trim: true
}
});
mongoose.model('User', UserSchema);
claim.server.model.js
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Claim Schema
*/
var ClaimSchema = new Schema({
description: {
type: String,
default: '',
required: 'Please fill Claim description',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.Types.ObjectID,
ref: 'User'
}
});
mongoose.model('Claim', ClaimSchema);
答案 0 :(得分:0)
如果您想在User
架构中使用Claim
架构,可以这样做:
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
User = require('./../models/user'); //proper path here
var UserSchema = require('mongoose').model('User').schema;
/**
* Claim Schema
*/
var ClaimSchema = new Schema({
description: {
type: String,
default: '',
required: 'Please fill Claim description',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: UserSchema,
ref: 'User'
}
});
mongoose.model('Claim', ClaimSchema);
和User
架构需要导出语句。将mongoose.model('User', UserSchema);
替换为:
exports.UserSchema = UserSchema;
module.exports = mongoose.model('User', UserSchema);
关于依赖项中正确路径的问题,'./../models/user'
应该适用于
├── server
| ├── models
| | └──user.server.model.js
| └── server.js