方法和静态:访问“字段". [英] Methods and statics: Accessing "fields"

查看:81
本文介绍了方法和静态:访问“字段".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下猫鼬模型:

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);

地址的定义是这样完成的:

The definition of the address is done like that:

module.exports = {
    street: String,
    ...
};

我这样做是出于可重用性.

I do this for reasons of reusability.

在我的业务逻辑中,我这样做:

And in my business logic I do this:

UserBusinessLogic.prototype.create = function(inputModel, callback) {
    var user = new UserPersistenceModel();
    user.firstname = inputModel.firstname;
    user.lastname = inputModel.lastname;
    ...

    user.save(function(error) {
        ...
        }
    });
};

我希望将输入模型传递给我的模型(在构造函数中),而不是将输入的所有值分配给业务逻辑中的模型:

Instead of assigning all the values from my input to the model in the business logic, I'd like to pass the input model to my model (in the constructor) like this:

var user = new UserPersistenceModel(inputModel);

应该读取输入中的所有值并将其分配给我模型的字段".

There all the values from the input should be read and assigned to the "fields" of my model.

为此,我考虑了方法和/或静态方法.据我了解,我应该在实例级别"上使用一种方法(我想保存一个文档),对吗?我的方法看起来如何?我不确定如何访问其中的字段.

To do so I thought about methods and/or statics. As far as I understood I should use a method as I'm working on the "instance level" (I want to save ONE document), right? How could my method look? I'm not sure how to access the fields in there.

更新

这是我的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 =地址;

module.exports = Address;

推荐答案

您已经拥有猫鼬User模型.您必须在业务逻辑中做些什么:

You already have the mongoose User model. What you have to to in your business logic:

// 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) {
    ...
  });
};

已更新

您错误地引用了地址模型.

You're referencing your address model incorrectly.

假设在Adress模型的末尾有module.exports = mongoose.model('Address', AddressEntityModel);行,这是您必须在User模型中引用它的方式:

Assuming that you have an module.exports = mongoose.model('Address', AddressEntityModel); line at the end of your Adress model, this is the way that you have to reference it in your User model:

var UserEntityModel = new Schema({
  firstname: String,
  lastname: String,
  address: { type: Schema.Types.ObjectId, ref: 'Address' }
});

您甚至不需要地址模型文件.

You don't even have to require the address model file.

猫鼬只存储引用对象中的id.因此,您可以更改引用地址中唯一的属性是id(例如清除引用或将其指向另一个地址).

Mongoose just stores the the id from the referenced object. So, the only attribute from the referenced address that you're able to change is the id (like cleaning the reference or pointing it to another address).

这篇关于方法和静态:访问“字段".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆