【问题标题】:ES6: Module '"mongoose"' has no default exportES6:模块“猫鼬”没有默认导出
【发布时间】:2019-12-29 08:09:33
【问题描述】:

使用 ES6 Imports & Exports 我觉得我应该能够将导入声明为 import mongoose, { Schema, Document } from 'mongoose'; 但我收到错误 Module '"mongoose"' has no default export.

以下内容确实有效,但显然不是进行此导入的正确方法。导出默认 id 也喜欢删除。

import * as mongoose from 'mongoose';
import { Schema, Document } from 'mongoose';

export interface IUser extends Document {
  email: string;
  password: string;
}

const UserSchema: Schema = new Schema({
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true }
});

export default mongoose.model<IUser>('User', UserSchema);

然后我将它与

一起使用
import UserModel, { IUser } from '../models/example'
import * as bcrypt from 'bcrypt';

class User {
  static register = async (req: Request, res: Response) => {
    const email = req.body.email;
    const password = req.body.password;
    const alreadyRegistered = await UserModel.findOne({email}).exec();
    if (!alreadyRegistered) {
      const hashedPassword = await bcrypt.hash(password,10);
        if (!hashedPassword) {
          res.status(500).send({ message: "Failed to encrypt your password" });
        } else {
          const user = new UserModel(<IUser>{email:email, password:hashedPassword});
          const saved = await user.save();
          if (!saved) {
            res.status(500).send({ message: "Failed to register you" });
          } else {
            res.status(200).send({ message: "You are now registered" });
          }
        }
    } else {
      res.status(400).send({ message: "You have already registered" });
    }
  };
}

export {User} 

【问题讨论】:

    标签: node.js typescript mongoose


    【解决方案1】:

    试试这个

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const UserSchema = new Schema({
      email: { type: String, required: true, unique: true },
      password: { type: String, required: true }
    });
    
    
    module.exports = mongoose.model('User', UserSchema);
    

    第二条路-

    import * as mongoose from 'mongoose';
    
    type UserSchema = IUser & mongoose.Document;
    
    const User = mongoose.model<UserSchema>('User', new mongoose.Schema({
            email  : { type: String, required: true, unique: true },
              password: { type: String, required: true }
        }));
    export = User;
    

    【讨论】:

    • mongoose.model&lt;IUser&gt;('User', UserSchema); 行现在给出Untyped function calls may not accept type arguments. 的错误
    • 如果您使用的是 express module.exports= mongoose.model('User', UserSchema),请使用 module.exports;并在同一个文件中导入 IUser
    • 我不太明白,您能否更新您的答案以反映这一点?
    • 检查一次答案然后告诉我
    • 第一个给出Untyped function calls may not accept type arguments.的错误第二个选项给出一个错误An export assignment cannot be used in a module with other exported elements.如果我不导出接口IUser然后我得到错误Cannot find name 'IUser'
    【解决方案2】:

    我不确定您是如何编译打字稿的,但它应该可以在 tsconfig.json 文件中设置的默认值下正常工作。

    • 通过运行tsc --init 在项目的根目录中生成tsconfig.json(如前所述,您可以使用默认值来处理目标集,甚至设置为es5

    • 现在您可以运行 tsc -wts-node index.ts 或者您更喜欢构建/运行您的代码

    请注意index.ts 文件必须位于项目的根目录中,否则您需要在tsconfig.json 中指定路径(rootDir 选项默认被注释掉并使用相对于@987654330 的路径@文件)。

    还请注意,如果您正在运行tsc -w 或其他一些从其他目录编译/运行代码的命令,而不是tsconfig.json 所在的目录,您将需要提供它的路径,即@987654333 @

    【讨论】:

      【解决方案3】:

      我遇到了这个问题,并通过将以下内容添加到 tsconfig.json 来修复它

      "esModuleInterop": true
      

      然后我可以正常导入 import mongoose from 'mongoose'

      【讨论】:

        【解决方案4】:

        我有同样的问题,我注意到我错过了tsconfig.json

        【讨论】:

          【解决方案5】:
          import { model, Schema } from 'mongoose';
          ...
          export default model<IUser>('User', UserSchema);
          

          【讨论】:

          • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
          【解决方案6】:

          确保您在tsconfig.json 文件中有"allowSyntheticDefaultImports": true,

          参考:https://github.com/microsoft/TypeScript-React-Starter/issues/8#issuecomment-301265017

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-12-22
            • 2016-05-15
            • 2021-11-24
            • 2017-03-18
            • 2017-10-26
            • 2018-10-02
            • 1970-01-01
            • 2018-07-25
            相关资源
            最近更新 更多