方法和静态:访问 "fields"
Methods and statics: Accessing "fields"
我有以下猫鼬模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var addressDefinition = require('./addressPersistenceModel');
var UserEntityModel = new Schema({
firstname: String,
lastname: String,
address: addressDefinition
});
mongoose.model('User', UserEntityModel);
地址的定义是这样完成的:
module.exports = {
street: String,
...
};
我这样做是出于可重用性的原因。
在我的业务逻辑中我这样做:
UserBusinessLogic.prototype.create = function(inputModel, callback) {
var user = new UserPersistenceModel();
user.firstname = inputModel.firstname;
user.lastname = inputModel.lastname;
...
user.save(function(error) {
...
}
});
};
我不想将输入中的所有值分配给业务逻辑中的模型,而是想像这样将输入模型传递给我的模型(在构造函数中):
var user = new UserPersistenceModel(inputModel);
应该读取输入中的所有值并将其分配给我的模型的 "fields"。
为此,我考虑了方法 and/or 静态。据我所知,我应该在处理 "instance level" 时使用一种方法(我想保存一个文档),对吗?我的方法看起来如何?我不确定如何访问其中的字段。
更新
这是我的 UserCreateInputModel
的样子:
var Address = require('../address');
var UserCreateInputModel = function(req) {
this.alias = req.param('alias');
this.email = req.param('email');
this.firstName = req.param('firstName');
this.lastName = req.param('lastName');
this.password = req.param('password');
this.address = new Address(req);
};
module.exports = UserCreateInputModel;
地址是这样的:
var Address = function(req, persistenceModel) {
if(req !== null && req !== undefined) {
this.city = req.param('city');
this.country = req.param('country');
this.state = req.param('state');
this.street = req.param('street');
this.zipCode = req.param('zipCode');
}
if(persistenceModel !== null && persistenceModel !== undefined) {
this.city = persistenceModel.city;
this.country = persistenceModel.country;
this.state = persistenceModel.state;
this.street = persistenceModel.street;
this.zipCode = persistenceModel.zipCode;
}
};
module.exports = 地址;
您已经有了 mongoose User
模型。您在业务逻辑中必须要做的事情:
// assuming you're doing a 'module.exports = mongoose.model('User', UserEntityModel);' in your schema file
var UserPersistenceModel = require('./your_user_schema_file.js');
UserBusinessLogic.prototype.create = function(inputModel, callback) {
// if your inputModel passed to this function is a javascript object like:
// {
// firstName: "First",
// lastName: "Last",
// ...
// }
var user = new UserPersistenceModel(inputModel);
...
user.save(function(error) {
...
});
};
已更新
您引用的地址模型不正确。
假设您在 Adress
模型的末尾有一个 module.exports = mongoose.model('Address', AddressEntityModel);
行,这是您必须在 User
模型中引用它的方式:
var UserEntityModel = new Schema({
firstname: String,
lastname: String,
address: { type: Schema.Types.ObjectId, ref: 'Address' }
});
您甚至不需要地址模型文件。
Mongoose 只存储来自引用对象的 id
。因此,您可以更改的引用地址的唯一属性是 id
(例如清理引用或将其指向另一个地址)。
我有以下猫鼬模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var addressDefinition = require('./addressPersistenceModel');
var UserEntityModel = new Schema({
firstname: String,
lastname: String,
address: addressDefinition
});
mongoose.model('User', UserEntityModel);
地址的定义是这样完成的:
module.exports = {
street: String,
...
};
我这样做是出于可重用性的原因。
在我的业务逻辑中我这样做:
UserBusinessLogic.prototype.create = function(inputModel, callback) {
var user = new UserPersistenceModel();
user.firstname = inputModel.firstname;
user.lastname = inputModel.lastname;
...
user.save(function(error) {
...
}
});
};
我不想将输入中的所有值分配给业务逻辑中的模型,而是想像这样将输入模型传递给我的模型(在构造函数中):
var user = new UserPersistenceModel(inputModel);
应该读取输入中的所有值并将其分配给我的模型的 "fields"。
为此,我考虑了方法 and/or 静态。据我所知,我应该在处理 "instance level" 时使用一种方法(我想保存一个文档),对吗?我的方法看起来如何?我不确定如何访问其中的字段。
更新
这是我的 UserCreateInputModel
的样子:
var Address = require('../address');
var UserCreateInputModel = function(req) {
this.alias = req.param('alias');
this.email = req.param('email');
this.firstName = req.param('firstName');
this.lastName = req.param('lastName');
this.password = req.param('password');
this.address = new Address(req);
};
module.exports = UserCreateInputModel;
地址是这样的:
var Address = function(req, persistenceModel) {
if(req !== null && req !== undefined) {
this.city = req.param('city');
this.country = req.param('country');
this.state = req.param('state');
this.street = req.param('street');
this.zipCode = req.param('zipCode');
}
if(persistenceModel !== null && persistenceModel !== undefined) {
this.city = persistenceModel.city;
this.country = persistenceModel.country;
this.state = persistenceModel.state;
this.street = persistenceModel.street;
this.zipCode = persistenceModel.zipCode;
}
};
module.exports = 地址;
您已经有了 mongoose User
模型。您在业务逻辑中必须要做的事情:
// assuming you're doing a 'module.exports = mongoose.model('User', UserEntityModel);' in your schema file
var UserPersistenceModel = require('./your_user_schema_file.js');
UserBusinessLogic.prototype.create = function(inputModel, callback) {
// if your inputModel passed to this function is a javascript object like:
// {
// firstName: "First",
// lastName: "Last",
// ...
// }
var user = new UserPersistenceModel(inputModel);
...
user.save(function(error) {
...
});
};
已更新
您引用的地址模型不正确。
假设您在 Adress
模型的末尾有一个 module.exports = mongoose.model('Address', AddressEntityModel);
行,这是您必须在 User
模型中引用它的方式:
var UserEntityModel = new Schema({
firstname: String,
lastname: String,
address: { type: Schema.Types.ObjectId, ref: 'Address' }
});
您甚至不需要地址模型文件。
Mongoose 只存储来自引用对象的 id
。因此,您可以更改的引用地址的唯一属性是 id
(例如清理引用或将其指向另一个地址)。