为什么我会收到"mongoose.connect不是函数"的消息?试图与猫鼬建立联系时? [英] Why am I getting "mongoose.connect is not a function" when trying to connect with mongoose?

查看:563
本文介绍了为什么我会收到"mongoose.connect不是函数"的消息?试图与猫鼬建立联系时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Node.js的新手,正在尝试使用TypeScript构建node/express/mongoose服务器应用程序.

I am new to Node.js and am trying to build a node/express/mongoose server app with TypeScript.

这是我的app.ts文件:

Here is my app.ts file:

// lib/app.ts
import express from 'express';
import * as bodyParser from 'body-parser';
import { Routes } from './routes/crmRoutes';
import * as mongoose from "mongoose";

class App {
  public app: express.Application;
  public routePrv: Routes = new Routes();
  public mongoUrl: string = 'mongodb://localhost/TodosDB';

  constructor() {
    this.app = express();
    this.config();
    this.routePrv.routes(this.app);
    this.mongoSetup();
  }

  private mongoSetup(): void {
    mongoose.connect(this.mongoUrl, {
      useNewUrlParser: true,
      useUnifiedTopology: true
    });
  }

  private config(): void {
    // support application/json type post data
    this.app.use(bodyParser.json());
    //support application/x-www-form-urlencoded post data
    this.app.use(bodyParser.urlencoded({ extended: false }));
  }
}

export default new App().app;

但是,当我尝试编译我的应用程序时,我得到:

However, when I try to compile my application, I get:

TypeError: mongoose.connect is not a function

我已经用光了我所有的Google技能-没运气了.

I've used up all my Google skill -- no luck.

有人可以告诉我我在做什么错吗?

Can anyone tell me what I'm doing wrong?

推荐答案

这对我有用:替换

import * as mongoose from "mongoose";

使用

import mongoose = require("mongoose");

这篇关于为什么我会收到"mongoose.connect不是函数"的消息?试图与猫鼬建立联系时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆