<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 自定義schema類型 > 原文:[Creating a Basic Custom Schema Type](http://mongoosejs.com/docs/customschematypes.html) > 翻譯:小蝦米(QQ:509129) ## 創建一個基本的自定義模式類型 在Mongoose 4.4.0的新特性:Mongoose支持自定義類型。在你到達一個自定義的類型之前,然而,知道一個自定義類型是大多數情況下矯枉過正。你可以用最基本的任務采用[自定義的`getters/setters`](http://mongoosejs.com/docs/2.7.x/docs/getters-setters.html),[虛函數](http://mongoosejs.com/docs/guide.html#virtuals),和[單一的嵌入式文檔](http://mongoosejs.com/docs/subdocs.html#single-embedded)。 讓我們看看一個基本模式類型例子:一個字節的整數。創建一個新的模式類型,你需要繼承`mongoose.SchemaType`和添加相應的屬性到`mongoose.Schema.Types`。你需要實現一個方法是`cast()`方法。 ``` function Int8(key, options) { mongoose.SchemaType.call(this, key, options, 'Int8'); } Int8.prototype = Object.create(mongoose.SchemaType.prototype); // `cast()` takes a parameter that can be anything. You need to // validate the provided `val` and throw a `CastError` if you // can't convert it. Int8.prototype.cast = function(val) { var _val = Number(val); if (isNaN(_val)) { throw new Error('Int8: ' + val + ' is not a number'); } _val = Math.round(_val); if (_val < -0x80 || _val > 0x7F) { throw new Error('Int8: ' + val + ' is outside of the range of valid 8-bit ints'); } return _val; }; // Don't forget to add `Int8` to the type registry mongoose.Schema.Types.Int8 = Int8; var testSchema = new Schema({ test: Int8 }); var Test = mongoose.model('Test', testSchema); var t = new Test(); t.test = 'abc'; assert.ok(t.validateSync()); assert.equal(t.validateSync().errors['test'].name, 'CastError'); assert.equal(t.validateSync().errors['test'].message, 'Cast to Int8 failed for value "abc" at path "test"'); assert.equal(t.validateSync().errors['test'].reason.message, 'Int8: abc is not a number'); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看