标签: setter

为什么我的Mongoose 3.8.7模式getter和setter被忽略了?

在使用Node.js,Mongoose和MongoDB时,我发现当我执行findOne查询时,我的Mongoose模式getter和setter不会触发.

我发现一个旧线程表明版本2.x中存在getter和setter的问题,但它表示它已经解决,我使用的是最新版本的Mongoose(3.8.7).

这是我的架构的一部分

function testGetter(value) {
        return value + " test";
}

/**
* Schema
*/

var schema = new Schema({
        username: { type: String, required: true, unique: true, get: testGetter }
});

// I have also tried this.

schema.path('username').get(function (value, schemaType) {
        return value + " test";
});
Run Code Online (Sandbox Code Playgroud)

这是我执行查询的方式

Model
.findOne(conditions, fields, options)
.populate(population)
.exec(function (error, doc) {
        callback(doc, error);
});
Run Code Online (Sandbox Code Playgroud)

它使用缺少"测试"后修复的用户名值进行响应.我在这里做错了吗?任何帮助将不胜感激!

附加信息

这是找到一个的结果:

{
    "username": "Radius"
}
Run Code Online (Sandbox Code Playgroud)

在应用上述两种方法之一后,这是schema.paths.username.getters的值:

[ [Function: testGetter] ]
Run Code Online (Sandbox Code Playgroud)

getter setter schema mongoose node.js

11
推荐指数
2
解决办法
7145
查看次数

Getter/Setter或其他地方的数据验证?

我想知道在gettersetter或代码中的其他地方进行验证是否是个好主意.

优化加速代码方面,这可能会让您感到惊讶,我认为您不应该在getter和setter中进行验证,而应该在您更新文件或数据库的代码中进行验证.我错了吗?

verification getter setter optimization

10
推荐指数
1
解决办法
2086
查看次数

getter和setter,指针或引用,以及在c ++中使用的良好语法?

我想知道C++ getter和setter的良好语法.

private:
YourClass *pMember;
Run Code Online (Sandbox Code Playgroud)

我猜这个setter很简单:

void Member(YourClass *value){
  this->pMember = value; // forget about deleting etc
}
Run Code Online (Sandbox Code Playgroud)

和吸气剂?我应该使用引用还是常量指针?

例:

YourClass &Member(){
   return *this->pMember;
}
Run Code Online (Sandbox Code Playgroud)

要么

YourClass *Member() const{
  return this->member;
}
Run Code Online (Sandbox Code Playgroud)

他们之间有什么区别?

谢谢,

编辑:

对不起,我将编辑我的问题...我知道引用和指针,我问的是引用和常量指针,作为getter,它们在我的代码中会有什么区别,比如在未来,我希望什么样的shoud如果我去某种方式会失败...

所以我想我会使用const指针而不是引用

const指针不能删除或设置,对吧?

c++ getter setter pointers reference

10
推荐指数
3
解决办法
2万
查看次数

如何从__init__调用属性设置器

我有以下大块的python代码:

import hashlib

class User:
    def _set_password(self, value):
        self._password = hashlib.sha1(value).hexdigest()

    def _get_password(self):
        return self._password

    password = property(
        fset = _set_password,
        fget = _get_password)

    def __init__(self, user_name, password):
        self.password = password

u = User("bob", "password1")
print(u.password)
Run Code Online (Sandbox Code Playgroud)

理论上应该打印出密码的SHA1,但是从构造函数设置self.password会忽略已定义的属性,只需将值设置为"password1"即可.然后print语句读取"password1"的值.

我知道这是在类和实例上定义的密码,但我不知道如何正确表示它,所以它的工作原理.任何帮助,将不胜感激.

python setter init new-style-class

10
推荐指数
1
解决办法
2767
查看次数

Java - 应该通过getter和setter方法在构造函数中访问私有实例变量吗?

我知道私有实例变量是通过他们的公共getter和setter方法访问的.

但是当我在IDE的帮助下生成构造函数时,它直接初始化实例变量,而不是通过setter方法初始化它们.

Q1.因此,我应该为构造函数更改IDE生成的代码,以通过其setter方法初始化这些实例变量.

Q2.如果是,那么IDE为什么不以这种方式生成构造函数代码?

============================= EDITED ==================== ===================

  • 我使用Eclipse和Netbeans IDE

  • 这是一个普遍的问题.但正如@Lords所要求的那样,答案取决于我们的构造函数是公共的还是受保护的,还是私有的还是私有的?

java oop setter constructor

10
推荐指数
1
解决办法
4673
查看次数

Objective-C getter/setter

我正在尝试通过Objective-C教程.书中有这个例子:

@interface
{
 int width;
 int height;
 XYPoint *origin;
}
@property int width, height;
Run Code Online (Sandbox Code Playgroud)

我想,"嘿,XYPoint对象没有getter/setter.代码确实有用." 现在我要回答我自己的问题:).

我认为它是因为"原点"已经是一个指针,并且在"宽度"和"高度"的引擎盖下发生的事情是,将会创建一个指向它们的指针.

我是对的,还是我在说BS :)?

我只是不明白.这里主要是:

#import "Rectangle.h"
#import "XYPoint.h"
int main (int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Rectangle *myRect = [[Rectangle alloc] init];
    XYPoint *myPoint = [[XYPoint alloc] init];

    [myPoint setX: 100 andY: 200];
    [myRect setWidth: 5 andHeight: 8];

    myRect.origin = myPoint; 

    NSLog (@"Rectangle w = %i, h = %i",
           myRect.width, myRect.height); 

    NSLog (@"Origin at (%i, %i)",
           myRect.origin.x, myRect.origin.y); …
Run Code Online (Sandbox Code Playgroud)

getter setter objective-c

10
推荐指数
2
解决办法
7万
查看次数

Objective-C属性设置器信号如何失败?

假设您有一个具有复制语义的属性.如果复制方法失败,你应该在setter中做什么?(我认为这是一种可能性,因为副本通常以alloc/init组合开始,它可能会失败并返回nil.)Apple建议返回错误代码而不是使用异常,但是setter通常具有void返回类型.推荐的方法是什么?你如何表明发生了错误?

iphone setter memory-management properties objective-c

10
推荐指数
2
解决办法
587
查看次数

DDD和Getters and Setters的使用

我已经阅读了一些关于Getters和Setter使用的文章/帖子,以及它们如何帮助破解域模型对象中封装的目的.我理解不使用setter背后的逻辑 - 你允许客户端代码在对象业务规则和不变量的上下文之外操纵该对象的属性.

现在这位校长仍然困惑我.例如,如果我需要更改对象的成员变量的值,会发生什么?例如,如果某个人的姓名发生变化,我该如何在模型中反映出来?起初我想,为什么没有一个名为'ChangeName'的函数让我传入新名称,然后又可以改变内部'name'变量.嗯....那只是一个二传手不是吧!

我需要澄清的是 - 如果我要完全消除setter,那么在上述情况下,我是否应该完全依赖构造函数参数?我应该通过构造函数传递新的属性值来代替旧的属性值,之后我可以通过将对象传递给我拥有的任何持久性基础结构来持久化更改吗?

这两篇文章在这个讨论中很有用:

  1. http://kellabyte.com/tag/ddd/
  2. http://typicalprogrammer.com/?p=23

getter setter encapsulation domain-driven-design getter-setter

10
推荐指数
1
解决办法
2964
查看次数

Scala setters - 多个参数

我可以在setter中使用多个参数吗?

例如:

private var _a = 0
def a = _a
def a_= (b: Int, c: Int) = _a = b + c
Run Code Online (Sandbox Code Playgroud)

如果是,我该如何调用setter方法?

parameters setter scala

10
推荐指数
1
解决办法
590
查看次数

为什么我需要一个用于自动装配/注入字段的setter?

我有一个豆子:

    <bean id="BasketLogic" class="efco.logic.EfcoBasketLogic" autowire="byType">
        <property name="documentLogic" ref="DocumentLogic" />
        <property name="stateAccess" ref="StateAccess" />
        <property name="contextAccess" ref="ContextAccess" />
    </bean>

  <bean id="EfcoErpService" autowire="byType" class="efco.erp.service.EfcoErpServiceImpl">
    <constructor-arg ref="ErpConnector"/>
  </bean>
Run Code Online (Sandbox Code Playgroud)

documentLogic,stateAccesscontextAccess上田BasketLogicImpl

我没有 <context:component-scan />

EfcoBasketLogic.java:

public class EfcoBasketLogic extends BasketLogicImpl {

        @Inject
        private EfcoErpService erpService;
    ...
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

erpService为null,除非我提供setter.但为什么?我认为在自动装配发生的地方不需要安装器?可能是BasketLogicImpl对此负责吗?

setter spring inject autowired

10
推荐指数
1
解决办法
1万
查看次数