【问题标题】:What about the 'this' variable for models in embedded documents?嵌入式文档中模型的“this”变量怎么样?
【发布时间】:2014-03-05 11:39:17
【问题描述】:

我是 node.js 和sails 的新手,但它很简单,所以我喜欢它 :) 我实际上正在使用带有 MongoDB anssails-mongo 的sails Framework 0.10rc3。

我知道 waterline 的贡献者并不像 mongodb (https://github.com/balderdashy/sails-mongo/issues/44#issuecomment-35561040) 那样喜欢模型中的嵌入式文档,但无论如何,我想知道“this”变量在其中的工作原理以及如何检索当前元素在内部数组中。

这是一个模型示例(我们可以称之为 ProjetSpan):

module.exports = {

attributes: {

        proj: 
        {
            model:'Projet'
        },

        spans:
        {
            start:
            {
                type: 'DATETIME',
                required: true,
                datetime: true,
                before: function() {
                    if (this.end < this.proj.end)
                        return this.end;
                    else
                        return this.proj.end;
                }
            },

            end:
            {
                type: 'DATETIME',
                required: true,
                datetime: true,
                after: function() {
                    if (this.start > this.proj.start)
                        return this.start;
                    else
                        return this.proj.start;
                }
            }
        }
}

};

在这种情况下“this”将如何工作? 'this' 是一个跨度(所以 this.end 可以工作,而不是 this.proj.end)还是 'this' 是一个 ProjetSpan(所以 this.proj.end 可以工作,但不是 this.end)?

最后,如何让this.end(当前span中的变量)和this.proj.end(当前文档关联中的变量)在这个嵌入上下文中工作?

【问题讨论】:

    标签: javascript node.js mongodb sails.js waterline


    【解决方案1】:

    Waterline 根本不支持嵌入文档,除了提供 json 数据类型。因此,您的模型示例在 Sails 中不起作用,需要重写为:

    module.exports = {
    
       attributes: {
    
        proj: {
            model:'projet'
        },
    
        spans: {
            type: 'json'
        },
    
        before: function() {
    
           if (this.spans.end < this.proj.end) {
              return this.spans.end;
           } else {
              return this.proj.end;
           }
    
        },
    
        after: function() {
    
           if (this.spans.start > this.proj.start) {
              return this.spans.start;
           } else {
              return this.proj.start;
           }
    
        }
    
    
    
    }
    

    在实例方法中(比如这里的beforeafter),this 指的是整个实例对象。您需要通过检查来增强该代码,以确保 this.proj 是一个对象(即,它填充有 ProjetSpan.find({}).populate('project')),并且 this.spans.endthis.spans.start 确实存在(因为 Waterline 不验证嵌入的 JSON)。

    【讨论】:

    • 谢谢 :) 对于对象检查,我已经创建了 this.proj != null &amp;&amp; typeof this.proj === 'object'spans 呢?在这种状态下,它是一个对象、一个数组还是其他什么?
    • "spans" 将是您设置的任何 JSON 类型;很可能是一个对象或数组。不幸的是,它们都是typeof“对象”,但您可以使用sails.util.isArray() 来测试数组(sails.util 包装 Lodash)。
    • 酷谢谢 :) !我按照建议更改了我的模型,但是在某些时候,深度关联测试呢?像if ((this.projCollab != null &amp;&amp; typeof this.projCollab === 'object') &amp;&amp; (this.projCollab.proj != null &amp;&amp; typeof this.projCollab.proj === 'object')) { if (this.end &lt; this.projCollab.proj.end) return this.end; else return this.projCollab.proj.end; } 中的before 函数?我知道我们现在不能做我,我已经为此做了一些改变,但在某些情况下仍然需要它......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多