Node.js mongoose.connect错误TS2531:对象可能是';空';

Node.js mongoose.connect错误TS2531:对象可能是';空';,node.js,typescript,mongoose,Node.js,Typescript,Mongoose,我有一个内置typescript的node/express/mongoose应用程序。但是我对猫鼬有意见 我得到以下错误TS2531:对象可能为“null”。使用mongoose时出错 应用程序ts import express from 'express'; import logger from 'morgan'; import mongoose from 'mongoose'; import passport from 'passport'; import cors from "cors";

我有一个内置typescript的node/express/mongoose应用程序。但是我对猫鼬有意见

我得到以下错误
TS2531:对象可能为“null”。
使用mongoose时出错

应用程序ts

import express from 'express';
import logger from 'morgan';
import mongoose from 'mongoose';
import passport from 'passport';
import cors from "cors";

import Routes from './routes';
import Config from './config/config';

class App {

    public app: express.Application;
    public config: any;

    constructor() {

        this.app = express();

        this.environment();
        this.database();
        this.middleware();
        this.routes();

    }

    private environment(): void {

        this.config = new Config();

    }

    private database(): void {

        const uri: string = this.config.db.uri;
        const options: any = this.config.db.options;

            mongoose.connect(uri, options).then(

                () => {
                    console.log("MongoDB Successfully Connected On: " + this.config.db.uri)
                },
                (err: any) => {
                    console.error("MongoDB Error:", err);
                    console.log('%s MongoDB connection error. Please make sure MongoDB is running.');
                    process.exit();
                }

            );

    }

    private middleware(): void {

        this.app.use(cors());
        this.app.use(logger('dev'));
        this.app.use(express.json());
        this.app.use(express.urlencoded({ extended: true }));
        this.app.use(passport.initialize());

    }

    private routes(): void {

        const routes = new Routes(this.app);

    }

}

export default App;
我该如何妥善处理这件事

编辑

发布整个应用程序文件以提供更多上下文。错误特别指出了以下行
mongoose.connect(uri,选项)

src/app.ts:39:13-错误TS2531:对象可能为“null”

39 mongoose.connect(uri,选项)。然后( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


显然
mongoose.connect()
可能返回
null
,并且您的TypeScript配置为进行严格的空值检查(这是一件好事!),因此您需要通过添加感叹号来“发誓”它不是空值:

mongoose.connect(uri, options)!.then(...

但是,如果返回值恰好是
null
,这将在运行时崩溃。

显然
mongoose.connect()
可能返回
null
,并且您的TypeScript被配置为进行严格的空值检查(这是一件好事!),因此您需要通过添加感叹号来“发誓”它不是null:

mongoose.connect(uri, options)!.then(...

但是,如果返回值恰好为
null
,则会在运行时崩溃。

连接
函数的定义:

export function connect(uris: string, options: ConnectionOptions, callback: (err: mongodb.MongoError) => void): null;
export function connect(uris: string, callback: (err: mongodb.MongoError) => void): null;
export function connect(uris: string, options?: ConnectionOptions): Promise<Mongoose>;

connect
功能的定义:

export function connect(uris: string, options: ConnectionOptions, callback: (err: mongodb.MongoError) => void): null;
export function connect(uris: string, callback: (err: mongodb.MongoError) => void): null;
export function connect(uris: string, options?: ConnectionOptions): Promise<Mongoose>;

您在哪一行收到错误?首先,它突出显示所有这些
mongoose.connect(uri,选项)
Any ideas@titian cernicova dragomir没有足够的ideas信息有些东西可能是空的,编译器会告诉你在使用之前检查它。但是如果没有一个复制错误的最小示例或更完整的定义,我很难用完整的代码更新我的问题,如果你能看看我的非常感谢。@titian cernicova dragomirOn哪一行出现了错误?首先它突出显示了所有这些
mongoose.connect(uri,选项)
Any ideas@titian cernicova dragomir没有足够的ideas信息有些东西可能是空的,编译器会告诉你在使用之前检查它。但是如果没有一个复制错误的最小示例或更完整的定义,我很难用完整的代码更新我的问题,如果你能看看我的谢谢。@titian cernicova dragomirgenius,ty:)它工作了
const options:mongoose.ConnectionOptions
genius,ty:)它工作了
const options:mongoose.ConnectionOptions