node.js - TypeScript 的 Mongoose 导入不起作用

标签 node.js typescript mongoose

Node 和 Typescript 的新手。当我运行 tsc 时,我收到一个错误,指出 mongoose.connect 不是一个函数。

我有以下代码:

import express = require('express');
import * as mongoose from "mongoose";

/** Routes for the app */
import apiUserRouter from "./api/user"

class App{

   public express :express.Application


    constructor() {
        this.express = express()
        this.setupDb();
    }

    private setupDb() : void {
        var mongoDb = 'mongodb://127.0.0.1/my_database';
        mongoose.connect(mongoDb);
        var db = mongoose.connection;
        db.on('error', console.error.bind(console, 'MongoDB Connection error'));
    }
}

如果我改变

import * as mongoose from "mongoose"

import mongoose = require('mongoose');

然后一切正常。

我已经为类型运行了以下 npm 命令,因为我的理解是这应该可以解决问题。

npm install @types/mongoose --save

编辑:添加我的 packages.json

{
    "name": "nodejs-ts-test2",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "@types/express": "^4.11.1",
        "@types/mongoose": "^5.0.3",
        "typescript": "^2.7.2"
    },
    "dependencies": {
        "express": "^4.16.2",
        "mongoose": "^5.0.7"
    }
}

和 tsconfig.json:

{
    "compilerOptions": {
        "target": "es2015",
        "module": "commonjs",
        "outDir": "dist",
        "strict": true,
        "noImplicitAny": false,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true
    }
}

最佳答案

由于您没有共享您的 package.json 或 tsconfig,因此无法说出错误可能出在哪里。所以我为您共享的代码创建了新项目,这样就不会发生错误。将我共享的文件与您必须共享的文件进行比较,以缩小问题范围。

package.json

{
  "name": "mong_type",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.11.1",
    "@types/mongoose": "^5.0.3",
    "typescript": "^2.7.2"
  },
  "dependencies": {
    "express": "^4.16.2",
    "mongoose": "^5.0.7"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "outDir": "./dist",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "include": ["src"]
}

src/app.ts

import express from "express";
import mongoose from "mongoose";

class App {
  public express: express.Application;

  constructor() {
    this.express = express();
    this.setupDb();
  }

  private setupDb(): void {
    var mongoDb = "mongodb://127.0.0.1/my_database";
    mongoose.connect(mongoDb);
    var db = mongoose.connection;
    db.on("error", console.error.bind(console, "MongoDB Connection error"));
  }
}

关于node.js - TypeScript 的 Mongoose 导入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001632/

相关文章:

javascript - 如何将 tsd 定义自动链接到 bower 和 npm?

node.js - 使用 Mongoose 模式验证密码/确认密码

javascript - 为什么 Dispatcher 在 Flux 上发起不必要的事件?

windows - node.js:在新窗口中运行外部命令

javascript - 导入json Mongodb

angular - 在 Angular/Typescript 中用整数和字母按字母顺序对数组进行排序

d3.js - TypeScript 和 d3 域类型

javascript - 无法使用 upsert 删除键

node.js - 如何在 Mongoose 中使用预保存 Hook 数据库值?

javascript - 引用错误: filename is not defined