【问题标题】:NestJS serving JSON and adds a "default" section repeating the JSONNestJS 提供 JSON 并添加重复 JSON 的“默认”部分
【发布时间】:2021-05-11 08:42:06
【问题描述】:

我在 NestJS 的一个端点上有一个奇怪的行为,它提供一段 JSON。 带有 JSON 对象的 JS 正在导出

module.exports = Object.freeze({
    translation: {
        TestMessage: 'Bienvenue à React et react-i18next'
    }
});

客户端上的结果是:

{
    "translation": {
        "TestMessage": "Bienvenue à React et react-i18next"
    },
    "default": {
        "translation": {
            "TestMessage": "Bienvenue à React et react-i18next"
        }
    }
}

问题是“默认”从何而来?

在模块、控制器和服务下方绘制全图:

模块

import { Module } from '@nestjs/common';

import { LoggerService } from '@modules/logger';
import { I18nController } from './i18n.controller';
import { I18nService } from './i18n.service';

@Module({
    controllers: [I18nController],
    providers: [I18nService, LoggerService],
    exports: [I18nService]
})
export class I18nModule {}

控制器

import { Controller, Get, Param } from '@nestjs/common';

import { LoggerService } from '@modules/logger';
import { I18nService } from './i18n.service';

@Controller('i18n')
export class I18nController {
    constructor(private logger: LoggerService, private i18nService: I18nService) {
        this.logger.setContext(I18nController.name);
    }

    @Get('/:lang')
    async getLanguage(@Param('lang') lang: string) {
        console.log(lang);
        return await this.i18nService.findOneByLanguageCode(lang);
    }
}

服务

import { Injectable } from '@nestjs/common';

import { access } from 'fs/promises';
import { constants as fsconstants } from 'fs';

@Injectable()
export class I18nService {
    async findOneByLanguageCode(language: string): Promise<any | null> {
        const languagefile = __dirname + '/../../public/languages/' + language + '.js';
        await access(languagefile, fsconstants.R_OK);
        return await import(languagefile);
    }
}

从客户端我做了一个简单的 http://localhost:3001/i18n/fr-FR 并得到上述结果。

再次,“默认”部分来自哪里?

【问题讨论】:

  • 您的tsconfig 中是否恰好有allowSyntheticDefaultImports?这看起来像 import 正在读取 default 导出,因此 JSON 中的 default
  • 是的!!,就是这样。 @JayMcDoniel,你是英雄。非常感谢。在有关 allowSyntheticDefaultImports (typescriptlang.org/tsconfig#allowSyntheticDefaultImports) 的文档中:“为方便起见,如果没有创建,像 Babel 这样的编译器会自动创建一个默认值。”

标签: nestjs


【解决方案1】:

应该在您的tsconfig.json 中启用esModuleInterop

https://www.typescriptlang.org/tsconfig#esModuleInterop

类似 import moment from "moment" 的默认导入与 const moment = require("moment").default 作用相同

这就是为什么你有 default 对象存在。

tsconfig.json

{
  "compilerOptions": {
     "esModuleInterop": true, // change it to false or remove it
  }
}

【讨论】:

  • 我已经将 esModuleInterop 设置为 True。解决方案是 allowSyntheticDefaultImports ,它使额外的默认部分。感谢您努力回答我的问题。
  • 我的意思是设置"esModuleInterop": false,在我的情况下,"esModuleInterop": false 解决了问题,但没有设置allowSyntheticDefaultImports。但是您会发现 allowSyntheticDefaultImports: false 对您有帮助,所以感谢 @JayMcDoniel 的帮助和欢呼!
猜你喜欢
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2014-03-30
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多