用打字稿创建猫鼬插件

时间:2018-12-16 08:01:21

标签: node.js mongodb typescript mongoose plugins

我在互联网上发现了有关猫鼬插件的有趣article。从该站点我得到了以下代码:

    function HidePlugin(schema) {
  var toHide = [];
  schema.eachPath(function(pathname, schemaType) {
    if (schemaType.options && schemaType.options.hide) {
      toHide.push(pathname);
    }    
  });
  schema.options.toObject = schema.options.toObject || {};
  schema.options.toObject.transform = function(doc, ret) {
    // Loop over all fields to hide
    toHide.forEach(function(pathname) {
      // Break the path up by dots to find the actual
      // object to delete
      var sp = pathname.split('.');
      var obj = ret;
      for (var i = 0; i < sp.length - 1; ++i) {
        if (!obj) {
          return;
        }
        obj = obj[sp[i]];
      }
      // Delete the actual field
      delete obj[sp[sp.length - 1]];
    });

    return ret;
  };
}

问题是我将typescript与node.js一起使用。打字稿编译器给我错误,因为没有显式类型。实际上,我不确切知道我必须将哪种类型归因于代码中的变量

0 个答案:

没有答案