【问题标题】:import sql from 'mssql'; console.log(sql); // returns undefined从'mssql'导入sql;控制台.log(sql); // 返回未定义
【发布时间】:2021-11-29 07:19:45
【问题描述】:

我正在尝试使用 SQL Server 在 Nest.js 中创建一个 CRUD。我已经安装了以下软件包:

mssqlhttps://www.npmjs.com/package/mssql

@types/mssqlhttps://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.

引起我注意的是sqldatabase.service.ts 中是未定义 我该如何解决这个问题?我做错了什么?

【问题讨论】:

    标签: node.js sql-server typescript express nestjs


    【解决方案1】:

    试试import * as sql from 'mssql';

    【讨论】:

    • 不起作用。 :(
    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。您可以在帮助中心找到更多关于如何写好答案的信息:stackoverflow.com/help/how-to-answer。祝你好运?
    【解决方案2】:

    我刚刚修好了!

    我在 tsconfig.json 中添加了"esModuleInterop": true,,问题就解决了。不知道为什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      相关资源
      最近更新 更多