启动服务器时,出现此错误:
this.mongo.connect('mongodb:// localhost:27017 / tsnode',{ ^ TypeError:无法读取未定义的属性“ connect”
import express from 'express';
import mongoose from 'mongoose';
import cors from 'cors';
class App {
public express: express.Application;
public mongo: mongoose.Mongoose;
constructor() {
this.express = express();
this.database();
this.middlewares();
this.routes();
}
private middlewares(): void {
this.express.use(express.json());
this.express.use(cors());
}
private database(): void {
this.mongo.connect('mongodb://localhost:27017/tsnode', {
useUnifiedTopology: true,
});
}
private routes(): void {
this.express.get('/', (req, res) => res.send('Hello World!'));
}
}
export default new App().express;
答案 0 :(得分:0)
您尚未启动mongo变量,因此应在构造函数中执行。 喜欢:
constructor() {
this.express = express();
this.mongo= mongoose; // initialize mongo variable
this.database();
this.middlewares();
this.routes();
}