我正在尝试用Server在Nest.js中创建CRUD。我已安装下列套件:
mssql https://www.npmjs.com/package/mssql
@types/mssql https://www.npmjs.com/package/@types/mssql
我已经创建了一个服务database.service.ts,其中有以下代码:
import { Injectable } from '@nestjs/common';
import sql from 'mssql';
@Injectable()
export class DatabaseService {
config = {
user: 'mssql',
password: 'password',
server: 'localhost',
database: 'atm',
options: {
encrypt: true,
trustServerCertificate: true,
},
};
async getPool() {
console.log('SQL obj', JSON.stringify(sql));
const pool = await sql.connect(this.config);
return pool;
}
}在app.controller.ts中,我有以下内容:
import { DatabaseService } from './database/database.service';
import { Controller, Get } from '@nestjs/common';
@Controller()
export class AppController {
constructor(private db: DatabaseService) {}
@Get()
getHello() {
this.db.getPool().then((pool) => {
pool
.request()
.query('SELECT 1')
.then((res) => {
return res;
});
});
}
}然后运行npm run start并使用Postman向端口3000发出以下GET请求:

如您所见,我没有得到任何响应,而且我的Nest.js服务器也崩溃了。

完整日志:
> cajero-backend@0.0.1 start C:\Users\carlo\Desktop\cajero\backend\cajero-backend
> nest start
[Nest] 11944 - 09/10/2021 16:11:32 LOG [NestFactory] Starting Nest application...
[Nest] 11944 - 09/10/2021 16:11:32 LOG [InstanceLoader] AppModule dependencies initialized +29ms
[Nest] 11944 - 09/10/2021 16:11:32 LOG [RoutesResolver] AppController {/}: +7ms
[Nest] 11944 - 09/10/2021 16:11:32 LOG [RouterExplorer] Mapped {/, GET} route +2ms
[Nest] 11944 - 09/10/2021 16:11:32 LOG [NestApplication] Nest application successfully started +1ms
SQL obj undefined
(node:11944) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'connect' of undefined
at DatabaseService.getPool (C:\Users\carlo\Desktop\cajero\backend\cajero-backend\src\database\database.service.ts:19:28)
at AppController.getHello (C:\Users\carlo\Desktop\cajero\backend\cajero-backend\src\app.controller.ts:15:13)
at C:\Users\carlo\Desktop\cajero\backend\cajero-backend\node_modules\@nestjs\core\router\router-execution-context.js:38:29
at InterceptorsConsumer.intercept (C:\Users\carlo\Desktop\cajero\backend\cajero-backend\node_modules\@nestjs\core\interceptors\interceptors-consumer.js:11:20)
at C:\Users\carlo\Desktop\cajero\backend\cajero-backend\node_modules\@nestjs\core\router\router-execution-context.js:46:60
at C:\Users\carlo\Desktop\cajero\backend\cajero-backend\node_modules\@nestjs\core\router\router-proxy.js:9:23
at Layer.handle [as handle_request] (C:\Users\carlo\Desktop\cajero\backend\cajero-backend\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\carlo\Desktop\cajero\backend\cajero-backend\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\carlo\Desktop\cajero\backend\cajero-backend\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\carlo\Desktop\cajero\backend\cajero-backend\node_modules\express\lib\router\layer.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11944) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:11944) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.吸引我注意的是,sql是未定义的 in database.service.ts,如何解决这个问题?我做错了什么?
发布于 2021-10-09 23:25:36
我刚修好了!
我在"esModuleInterop": true,中添加了tsconfig.json,问题解决了。不知道为什么。
发布于 2021-10-09 22:52:55
试试import * as sql from 'mssql';
https://stackoverflow.com/questions/69510976
复制相似问题