猫鼬,打字稿上的导入差异

时间:2020-08-09 19:01:16

标签: node.js mongodb typescript mongoose

我有一些麻烦的打字稿和猫鼬

我有很多文件用于配置猫鼬

Connections.ts

import { connect } from 'mongoose';
import './Plugins';

  const options = { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false };
  Bluebird.resolve(connect(uri, options, (err) => (err ? reject(err) : resolve())))

Plugins.ts

import Bluebird from 'bluebird';
import mongoose from 'mongoose';

mongoose.Promise = Bluebird;

mongoose.plugin((schema: any) => {
  const json = {
    virtuals: true,
    transform(doc: any, converted: any, options: any) {
      const { paths } = schema;
      for (const path in paths) {
        if (paths[path].options && paths[path].options.private) {
          if (converted[path]) {
            delete converted[path];
          }
        }
      }
      delete converted._id;
    },
  };
  schema.options.versionKey = false;
  schema.options.timestamps = true;
  schema.options.toJSON = { ...schema.options.toJSON, ...json };
});

一切正常,连接成功建立

当我创建模式和模型时

import { Document, model, Schema } from 'mongoose';
import { ObjectId } from 'mongodb';

export class User {
  id: ObjectId;
  password: string;
  email: string;
}

export interface UserDocument extends User, Document {
  id: ObjectId;
}

const UserSchema: Schema = new Schema<UserDocument>({
  password: { type: String, required: true, private: true },
  email: { type: String, required: true, index: true },
});

UserSchema.pre<UserDocument>('save', async function (next) {
  if (this.isModified('password')) return next();
  return passwordEncoder
    .hash(this.password)
    .tap((hashPassword) => (this.password = hashPassword))
    .then(() => next());
});

UserSchema.methods.validatePassword = async function (password: string) {
  return passwordEncoder.compare(password, this.password).then((isPassword) => (isPassword ? Promise.resolve() : httpError(400, 'InvalidPassword')));
};

export default model<UserDocument>('user', UserSchema);

现在出现错误

如果我导入猫鼬默认其他,例如import mongoose, { connect, plugin } from 'mongoose';

TypeError:TypeError:mongoose_1.connect不是函数

如果一切正常,UserModel不会解析插件以及挂钩,静态变量或方法

我认为这与猫鼬对象的导入方式有关,但目前我不知道该怎么做,因为创建了模型,它会搜索数据库以及所有内容,但插件和其他方法无法解决,打字稿标记错误

我的tsconfig.json

{
  "compilerOptions": {
    "incremental": true,
    "target": "ES2015",
    "module": "commonjs",
    "allowJs": true,
    "declaration": true,
    "sourceMap": false,
    "outDir": "./dist",
    "removeComments": false,
    "strict": true,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "strictPropertyInitialization": false,
    "noUnusedLocals": false,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "allowSyntheticDefaultImports": true
  },
  "ts-node": {
    "transpileOnly": true,
    "files": true
  },
  "include": ["./src/**/*.ts", "./src/**/*.js"],
  "exclude": ["**/*/node_modules/*", "**/*.d.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.test.js", "**/*.spec.js"]
}

非常感谢

0 个答案:

没有答案