{"id":298,"date":"2017-05-04T17:46:09","date_gmt":"2017-05-04T09:46:09","guid":{"rendered":"https:\/\/colliot.me\/?p=298"},"modified":"2017-11-20T06:56:44","modified_gmt":"2017-11-19T22:56:44","slug":"%e4%b8%80%e4%b8%aa%e4%b8%8d%e6%ad%a3%e7%a1%ae%e7%9a%84-typescript-%e7%b1%bb%e5%9e%8b%e5%ae%9a%e4%b9%89%e5%bc%95%e5%8f%91%e7%9a%84%e8%a1%80%e6%a1%88","status":"publish","type":"post","link":"https:\/\/colliot.org\/ru\/2017\/05\/%e4%b8%80%e4%b8%aa%e4%b8%8d%e6%ad%a3%e7%a1%ae%e7%9a%84-typescript-%e7%b1%bb%e5%9e%8b%e5%ae%9a%e4%b9%89%e5%bc%95%e5%8f%91%e7%9a%84%e8%a1%80%e6%a1%88\/","title":{"rendered":"(\u4e2d\u6587) \u4e00\u4e2a\u4e0d\u6b63\u786e\u7684 TypeScript \u7c7b\u578b\u5b9a\u4e49\u5f15\u53d1\u7684\u8840\u6848"},"content":{"rendered":"<p class=\"qtranxs-available-languages-message qtranxs-available-languages-message-ru\">\u0418\u0437\u0432\u0438\u043d\u0438\u0442\u0435, \u044d\u0442\u043e\u0442 \u0442\u0435\u0445\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432 &ldquo;<a href=\"https:\/\/colliot.org\/zh\/wp-json\/wp\/v2\/posts\/298\" class=\"qtranxs-available-language-link qtranxs-available-language-link-zh\" title=\"\u4e2d\u6587\">\u4e2d\u6587<\/a>&rdquo; \u0438 &ldquo;<a href=\"https:\/\/colliot.org\/en\/wp-json\/wp\/v2\/posts\/298\" class=\"qtranxs-available-language-link qtranxs-available-language-link-en\" title=\"English\">\u0410\u043c\u0435\u0440\u0438\u043a\u0430\u043d\u0441\u043a\u0438\u0439 \u0410\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439<\/a>&rdquo;. For the sake of viewer convenience, the content is shown below in this site default language. You may click one of the links to switch the site language to another available language.<\/p><p>Mongoose \u7684 TypeScript \u7c7b\u578b\u5b9a\u4e49\u5e76\u4e0d\u5b8c\u5168\u6b63\u786e\uff01<\/p>\n<p>\u6628\u5929\u665a\u4e0a\u5728 bgs \u7814\u7a76\u4e86\uff08\u6216\u8005\u8bf4\u8c03\u8bd5\u4e86\uff09<a href=\"http:\/\/lingsamuel.github.io\">XGS<\/a> \u5199\u7684 nodejs \u670d\u52a1\u5668\u9879\u76ee\u3002\u4ed6\u5728\u7528 koa \u4f5c\u4e3a\u670d\u52a1\u5668\uff0cmongodb \u4f5c\u4e3a\u6570\u636e\u5b58\u50a8\uff0c\u5199\u4e00\u4e2a\u4e0d\u77e5\u9053\u4ec0\u4e48\u9879\u76ee\uff08\u4f3c\u4e4e\u540d\u4e3a\u300c\u5199\u4f5c\u8005\u5e73\u53f0\u300d\/ &#171;Write&#8217;s Platform&#187;\uff09\u3002\u53cd\u6b63\u5f53\u65f6\u8fd8\u5904\u4e8e\u5f88\u521d\u7ea7\u7684\u9636\u6bb5\uff0c\u53ea\u6709\u4e24\u4e2a\u8def\u7531\uff0c\u4e00\u4e2a get\uff0c\u4e00\u4e2a post\uff0c\u540e\u8005\u662f\u7528\u6765\u5411 mongodb \u63d0\u4ea4\u4e00\u7bc7\u6587\u7ae0\u7684\u3002\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u4ed6\u53d1\u73b0\u4e86\u4e00\u4e2a\u95ee\u9898\u2014\u2014\u4ed6\u5728\u4fdd\u5b58\u6587\u7ae0\u7684\u56de\u8c03\u4e2d\u66f4\u6539\u4e86\u54cd\u5e94\u7684\u5185\u5bb9\uff08\u5982\u4e0b\uff09\uff0c\u4f46\u662f\u5b9e\u9645\u6d4b\u8bd5\u5f97\u5230\u7684\u54cd\u5e94\u5185\u5bb9\u5374\u662f koa \u9ed8\u8ba4\u7684 OK\u3002<\/p>\n<pre class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"original code\">router.post('\/path', (ctx, next) =&gt; {\r\n    const article = new Article( \/* something *\/ );\r\n    article.save((err, article) =&gt; {\r\n      if (err){\r\n        ctx.body = err;\r\n      } else {\r\n        ctx.body = \"success\";\r\n      }\r\n    });\r\n})<\/pre>\n<p>\u8d77\u521d\u6211\u53d1\u73b0\uff0c\u4ed6\u7684\u4ee3\u7801\u4e2d\uff0c<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">router.post('\/path', (ctx, next) =&gt; { \/* body *\/ });<\/code>\u7684<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">\/* body *\/<\/code>\u8fd9\u6bb5\u4e2d\u5e76\u6ca1\u6709\u8fd4\u56de <code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">next()<\/code>\uff0c\u8fd9\u5e94\u8be5\u4f1a\u5bfc\u81f4\u5f02\u6b65\u7684\u56de\u8c03\u6ca1\u6709\u88ab\u963b\u585e\u3002<\/p>\n<p>\u63a5\u7740\u6211\u60f3\uff0c\u53ef\u80fd\u53c8\u662f koa \u7684\u7248\u672c\u641e\u6df7\u4e86\u4ec0\u4e48\u7684\uff0c\u56e0\u4e3a koa v2\uff0ckoa router \u4ec0\u4e48\u7684\u7248\u672c\u7ba1\u7406\u90fd\u5f88\u6df7\u4e71\uff0c\u957f\u671f\u5904\u4e8e\u867d\u7136\u5927\u5bb6\u90fd\u60f3\u4f7f\u7528\u6700\u65b0\u7248\uff0c\u4f46\u662f\u9ed8\u8ba4\u7684\u5374\u4e00\u76f4\u662f\u65e7\u7248\u7684\u60c5\u51b5\u3002\u540e\u6765\u53c8\u51fa\u73b0\u4e86 koa \u662f\u65b0\u7248\uff0crouter \u662f\u65e7\u7248\u7684\u60c5\u51b5\uff0c\u66fe\u7ecf\u6211\u4e3a\u522b\u4eba\u89e3\u51b3\u8fc7\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u4f46\u662f\u8fd9\u6b21\u5374\u4e0d\u662f\u3002\u5e76\u4e14\u628a\u8fd4\u56de promise \u5207\u6362\u4e3a\u4f7f\u7528 <code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">async\/await<\/code>\u4e4b\u540e\uff08\u5982\u4e0b\uff09\uff0c\u60c5\u51b5\u4f9d\u7136\u6ca1\u6709\u597d\u8f6c\u3002<\/p>\n<pre class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"3,7,11-13\" data-caption=\"using promise\">router.post('\/path', (ctx, next) =&gt; {\r\n    const article = new Article( \/* something *\/ );\r\n    return article.save((err, saved) =&gt; {\r\n      if (err){\r\n        ctx.body = err;\r\n      } else {\r\n        console.log(saved);\r\n        ctx.body = \"success\";\r\n      }\r\n      \/\/ or `return next();` here.\r\n    }).then((thing) =&gt; {\r\n        console.log(thing);\r\n        return next();\r\n    });\r\n})<\/pre>\n<p>\uff08\u8fc7\u7a0b\u4e2d\u6211\u4eec\u53d1\u73b0\u8fd9\u4e2a\u4f20\u7ed9\u56de\u8c03\u7684 <code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">thing<\/code>\u5c45\u7136\u662f<code>undefined<\/code>\uff0c\u8fd9\u662f\u4e2a\u4f0f\u7b14\uff09<\/p>\n<p>\u6e10\u6e10\u5730\u6211\u88ab\u5404\u79cd\u51fd\u6570\u7684\u7b7e\u540d\uff08\u6bd4\u5982\u7a76\u7adf\u5e94\u8be5\u8fd4\u56de\u4e00\u4e2a\u4ec0\u4e48\uff0c\u662f next() \u8fd8\u662f\u4e00\u4e2a promise\uff0c\u800c next \u7684\u7c7b\u578b\u53c8\u662f\u4ec0\u4e48\uff1f\uff09\u641e\u5f97\u70e6\u8e81\u8d77\u6765\u3002\u4e8e\u662f\u6211\u63d0\u8bae\u4f7f\u7528 TypeScript \u6765\u8f85\u52a9\u68c0\u67e5\u3002<\/p>\n<p>\u6211\u4eec\u5f00\u59cb\u6000\u7591 <code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">article.save<\/code>\u8fd9\u4e2a\u51fd\u6570\u3002\u56e0\u4e3a\u5728 koa \u5b98\u65b9\uff08\u4ec5\u6709\uff09\u7684\u3001\u6d89\u53ca\u5230\u7b49\u5f85\u5f02\u6b65\u52a8\u4f5c\u7684 router \u4ee3\u7801\u6837\u4f8b\u91cc\uff0c\u5b8c\u5168\u7c7b\u4f3c\u7684\u4ee3\u7801\u662f\u53ef\u4ee5\u5de5\u4f5c\u7684\uff1a<\/p>\n<pre class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"3-7\" data-caption=\"koa router official demo\">router.get(\r\n  '\/users\/:id',\r\n  function (ctx, next) {\r\n    return User.findOne(ctx.params.id).then(function(user) {\r\n      ctx.user = user;\r\n      return next();\r\n\r\n    });\r\n  },\r\n  function (ctx) {\r\n    console.log(ctx.user);\r\n    \/\/ =&gt; { id: 17, name: \"Alex\" }\r\n  }\r\n);<\/pre>\n<p>\u4e8e\u662f\u6211\u4eec\u7740\u91cd\u68c0\u67e5\u4e86 mongoose \u7684 TypeScript \u7c7b\u578b\u5b9a\u4e49\u6587\u4ef6\u3002\u8fd9\u4e2a <code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">article<\/code>\u662f<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">Article<\/code>\u7684\u5b9e\u4f8b\uff0c\u800c<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">Article<\/code>\u5b9e\u9645\u4e0a\u662f mongoose \u52a8\u6001\u6784\u9020\u51fa\u6765\u7684\u4e00\u4e2a\u7c7b\uff08\u5e94\u8be5\u662f\u901a\u8fc7\u4e86\u67d0\u79cd\u5143\u7f16\u7a0b\u6280\u672f\uff09\u3002\u5728\u7c7b\u578b\u5b9a\u4e49\u4e2d\u662f\u8fd9\u4e48\u53cd\u6620\u7684\uff1a<\/p>\n<pre class=\"prettyprint lang-typescript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"type definition of &#96;Model&#96;\">interface Model&lt;T extends Document&gt; extends NodeJS.EventEmitter, ModelProperties {\r\n    new(doc?: Object): T;\r\n    \/* other things *\/\r\n}<\/pre>\n<p>\u800c article \u5c31\u662f\u90a3\u4e2a<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">&lt;T extends Document&gt;<\/code>\uff0c\u800c <code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">Document<\/code>\u7684<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">save<\/code>\u65b9\u6cd5\u662f\u8fd9\u4e48\u5199\u7684\uff1a<\/p>\n<pre class=\"prettyprint lang-typescript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"8-9\" data-caption=\"type definition of &#96;.save&#96;\">    \/**\r\n     * Saves this document.\r\n     * @param options options optional options\r\n     * @param options.safe overrides schema's safe option\r\n     * @param options.validateBeforeSave set to false to save without validating.\r\n     * @param fn optional callback\r\n     *\/\r\n    save(options?: SaveOptions, fn?: (err: any, product: this, numAffected: number) =&gt; void): Promise&lt;this&gt;;\r\n    save(fn?: (err: any, product: this, numAffected: number) =&gt; void): Promise&lt;this&gt;;<\/pre>\n<p>\uff08\u53d6\u81ea <code class=\"prettyprint lang-powershell\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">npm install @types\/mongoose<\/code>\u7684\u7ed3\u679c\uff0c\u4e0d\u77e5\u4e3a\u4f55\u4e0e DefinitelyTyped \u4e0a\u7684\u6709\u6240\u4e0d\u540c\u3002\uff09\u770b\u5230\u8fd9\uff0c\u7279\u522b\u662f<code class=\"prettyprint lang-typescript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">Promise&lt;this&gt;<\/code>\uff0c\u6211\u5c31\u786e\u4fe1\u4e86\uff0c\u8fd9\u4e2a\u51fd\u6570\u6ca1\u6709\u95ee\u9898\u3002\u53ef\u662f\u4e0a\u9762\u8fd4\u56de\u7684<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">thing<\/code>\u53c8\u786e\u4e4e\u662f\u4e00\u4e2a<code>undefined<\/code>\uff0c\u96be\u9053\u662f\u6211\u4eec\u7684\u4f7f\u7528\u59ff\u52bf\u6709\u95ee\u9898\uff1f<\/p>\n<p>\u4e07\u822c\u65e0\u5948\u4e4b\u4e2d\uff0cTypeScript \u4e5f\u4e0d\u9760\u8c31\u4e4b\u9645\uff0c\u6211\u4eec\u53ea\u80fd\u8bc9\u8bf8\u6e90\u7801\u4e86\u3002\u9274\u4e8e mongoose \u7684\u4ee3\u7801\u98ce\u683c\uff0c\u76f4\u63a5\u641c\u7d22 <code>.prototype.save<\/code>\u5c31\u80fd\u627e\u5230\u3002\u5b83\u5b9e\u9645\u4e0a\u662f\u5728<a href=\"https:\/\/github.com\/Automattic\/mongoose\/blob\/773639adbde210945598fcd6cf11a65d418fe5f2\/lib\/model.js#L307\">\u8fd9\u91cc<\/a>\uff0c\u4f46\u662f\u770b\u5230\u8fd9\u91cc\u8ba9\u4eba\u66f4\u5d29\u6e83\u4e86\uff1a<\/p>\n<pre class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"15\" data-caption=\"source code of &#96;.save&#96;\">Model.prototype.save = function(options, fn) {\r\n  if (typeof options === 'function') {\r\n    fn = options;\r\n    options = undefined;\r\n  }\r\n\r\n  if (!options) {\r\n    options = {};\r\n  }\r\n\r\n  if (fn) {\r\n    fn = this.constructor.$wrapCallback(fn);\r\n  }\r\n\r\n  return this.$__save(options, fn);\r\n};<\/pre>\n<p>\u5b83\u8fd4\u56de\u7684\u662f<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">this.$__save<\/code><span class=\"pl-en\">\u7684\u7ed3\u679c\uff0c\u4f46<a href=\"https:\/\/github.com\/Automattic\/mongoose\/blob\/773639adbde210945598fcd6cf11a65d418fe5f2\/lib\/model.js#L210\">\u8fd9\u4e2a\u51fd\u6570<\/a>\u5e76\u6ca1\u6709\u8fd4\u56de\u503c\uff01\u65e5\u5fd7\u663e\u793a\uff0c\u8fd9\u91cc\u7684<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">this<\/code>\u5c31\u662f\u8fd9\u4e2a<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">article<\/code>\u5b9e\u4f8b\uff08\u4e00\u4e2a<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">Document<\/code>\uff09\u3002\u7136\u800c\u7ebf\u7d22\u5230\u8fd9\u91cc\u4f3c\u4e4e\u65ad\u4e86\u3002\u600e\u4e48\u80fd\u51ed\u7a7a\u4ece\u4e00\u4e2a\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a promise \u5462\uff1f<\/span><\/p>\n<p>\u6211\u4eec\u53c8\u60f3\u5230\u4e86\u4e00\u62db\uff1a\u67e5\u770b\u8c03\u7528\u6808\u3002\u4f46\u662f node \u7684\u8c03\u8bd5\u5668\u5728\u8fd9\u65f6\u5374\u4e0d\u597d\u4f7f\u4e86\u3002\u6211\u4eec\u53ea\u80fd\u7528\u539f\u59cb\u7c97\u66b4\u7684<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">console.trace<\/code>\u624b\u52a8\u641e\u51fa\u8c03\u7528\u6808\u3002\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u7684\u7ebf\u7d22\uff0c\u8fd9\u4e2a\u8c03\u7528\u6808\u7ed9\u4e86\u6211\u4eec\u4e00\u4e9b\u63d0\u793a\u2014\u2014<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">save<\/code>\u5e76\u4e0d\u662f\u76f4\u63a5\u8c03\u7528\u7684\uff0c\u800c\u662f\u7ecf\u8fc7\u4e86\u67d0\u79cd<code>hook<\/code>\u7684\u5c01\u88c5\u3002<\/p>\n<p>\u8fd9\u65f6\uff0c\u6211\u60f3\u5230\u4e86\u76f4\u63a5<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">console.log(article.save)<\/code>\uff0c\u5b83\u5c45\u7136\u662f\u4e00\u4e2a<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">wrappedPointCut<\/code>\u51fa\u6765\u7684\u7ed3\u679c\uff01\u518d\u5728\u4ee3\u7801\u91cc\u641c\u7d22<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">wrappedPointCut<\/code>\u5c31\u975e\u5e38\u63a5\u8fd1\u771f\u76f8\u4e86\u3002\u5173\u952e\u7684\u4ee3\u7801\u5728<a href=\"https:\/\/github.com\/Automattic\/mongoose\/blob\/fa2caf50a9c7c95a16e11f2aa95fa516b28cbce2\/lib\/services\/model\/applyHooks.js#L102\">\u8fd9\u91cc<\/a>\uff1a<\/p>\n<pre class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"7,33,21\" data-caption=\"wrapped pointcut\">model.prototype[pointCut] = (function(_newName) {\r\n      return function wrappedPointCut() {\r\n        var Promise = PromiseProvider.get();\r\n\r\n        var _this = this;\r\n        \/* something omitted for simplicity *\/\r\n        var promise = new Promise.ES6(function(resolve, reject) {\r\n          args.push(function(error) {\r\n            if (error) {\r\n              \/* something omitted for simplicity *\/\r\n              reject(error);\r\n              return;\r\n            }\r\n            $results = Array.prototype.slice.call(arguments, 1);\r\n            resolve.apply(promise, $results);\r\n          });\r\n          _this[_newName].apply(_this, args);\r\n        });\r\n        if (fn) {\r\n          \/* something omitted for simplicity *\/\r\n          return promise.then(\r\n            function() {\r\n              process.nextTick(function() {\r\n                fn.apply(null, [null].concat($results));\r\n              });\r\n            },\r\n            function(error) {\r\n              process.nextTick(function() {\r\n                fn(error);\r\n              });\r\n            });\r\n        }\r\n        return promise;\r\n      };\r\n    })(newName);<\/pre>\n<p>\u6ce8\u610f\u5230\u5982\u679c\u6ca1\u6709<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">fn<\/code>\uff0c\u90a3\u4e48\u4f1a\u5728\u7b2c 33 \u884c\u8fd4\u56de\u7b2c 7 \u884c\u5b9a\u4e49\u7684 promise\uff0c\u5982\u679c\u6709<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">fn<\/code>\uff0c\u5c31\u4f1a\u76f4\u63a5\u5728\u7b2c 21 \u884c\u8fd4\u56de\u63a5\u7eed\u4e4b\u524d\u7684 promise \u7684\u53e6\u4e00\u4e2a promise\uff0c\u8fd9\u4e2a\u63a5\u7eed\uff08<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">.then()<\/code>\uff09\u91cc\u9762\u5229\u7528\u4e86<code class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">fn<\/code>\u5904\u7406\u4e86\u7ed3\u679c\uff0c\u5374\u6ca1\u6709\u8fd4\u56de\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u65f6\u5019\u7684\u8fd4\u56de\u503c\u662f<code class=\"prettyprint lang-typescript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">Promise&lt;undefined&gt;<\/code>\u800c\u4e0d\u662f<code class=\"prettyprint lang-typescript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">Promise&lt;this&gt;<\/code>\u2014\u2014\u6211\u4eec\u88ab\u8fd9\u6837\u7684 TypeScript \u7c7b\u578b\u5b9a\u4e49\u5751\u60e8\u4e86\uff01\u5982\u679c\u5b83\u4e00\u5f00\u59cb\u5c31\u544a\u8bc9\u6211\u4eec\u4f1a\u8fd9\u6837\uff0c\u90a3\u8be5\u591a\u597d\u5440\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"prettyprint lang-typescript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"1-2\" data-caption=\"suggested correct type definition\">    save(options: SaveOptions, fn: (err: any, product: this, numAffected: number) =&gt; void): Promise&lt;undefined&gt;;\r\n    save(fn: (err: any, product: this, numAffected: number) =&gt; void): Promise&lt;undefined&gt;;\r\n    save(options?: SaveOptions): Promise&lt;this&gt;;\r\n    save(): Promise&lt;this&gt;;<\/pre>\n<p style=\"text-align: right;\">2017 \u5e74 5 \u6708 4 \u65e5<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>\u0418\u0437\u0432\u0438\u043d\u0438\u0442\u0435, \u044d\u0442\u043e\u0442 \u0442\u0435\u0445\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432 &ldquo;\u4e2d\u6587&rdquo; \u0438 &ldquo;\u0410\u043c\u0435\u0440\u0438\u043a\u0430\u043d\u0441\u043a\u0438\u0439 \u0410\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439&rdquo;. For the sake of viewer convenience, the content is shown below in this site default language. You may click one of the links to switch the site language to another available language.Mongoose \u7684 TypeScript \u7c7b\u578b\u5b9a\u4e49\u5e76\u4e0d\u5b8c\u5168\u6b63\u786e\uff01 \u6628\u5929\u665a\u4e0a\u5728 bgs \u7814\u7a76\u4e86\uff08\u6216\u8005\u8bf4\u8c03\u8bd5\u4e86\uff09XGS \u5199\u7684 nodejs \u670d\u52a1\u5668\u9879\u76ee\u3002\u4ed6\u5728\u7528 koa \u4f5c\u4e3a\u670d\u52a1\u5668\uff0cmongodb \u4f5c\u4e3a\u6570\u636e\u5b58\u50a8\uff0c\u5199\u4e00\u4e2a\u4e0d\u77e5\u9053\u4ec0\u4e48\u9879\u76ee\uff08\u4f3c\u4e4e\u540d\u4e3a\u300c\u5199\u4f5c\u8005\u5e73\u53f0\u300d\/ &#171;Write&#8217;s &hellip; <a href=\"https:\/\/colliot.org\/ru\/2017\/05\/%e4%b8%80%e4%b8%aa%e4%b8%8d%e6%ad%a3%e7%a1%ae%e7%9a%84-typescript-%e7%b1%bb%e5%9e%8b%e5%ae%9a%e4%b9%89%e5%bc%95%e5%8f%91%e7%9a%84%e8%a1%80%e6%a1%88\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#171;(\u4e2d\u6587) \u4e00\u4e2a\u4e0d\u6b63\u786e\u7684 TypeScript \u7c7b\u578b\u5b9a\u4e49\u5f15\u53d1\u7684\u8840\u6848&#187;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":318,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,16],"tags":[17,20,19,18,21],"_links":{"self":[{"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/posts\/298"}],"collection":[{"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/comments?post=298"}],"version-history":[{"count":0,"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/posts\/298\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/media\/318"}],"wp:attachment":[{"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/media?parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/categories?post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/colliot.org\/ru\/wp-json\/wp\/v2\/tags?post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}