我正在使用 NestJs 框架(顺便说一句,很喜欢它),我想检查传入的数据,使其符合 Typscript 中的枚举。所以我有以下内容:
enum ProductAction {
PURCHASE = 'PURCHASE',
}
@Patch('products/:uuid')
async patchProducts(
@Param('uuid', ParseUUIDPipe) uuid: string,
@Body('action', ParseEnumPipe) action: ProductAction,
) {
switch(action) {
... code
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,当我运行这段代码时,第一个管道被编译
2022-07-21 16:53:51 [error] [ExceptionHandler] Nest can't resolve dependencies of the ParseEnumPipe (?, Object). Please make sure that the argument Object at index [0] is available in the FriendsModule context.
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我尝试将 nanoid 导入 NestJS 并收到此错误:
错误 [ERR_REQUIRE_ESM]:ES 模块的 require() ....
... 而是将 ... 中的 index.js 的 require 更改为所有 CommonJS 模块中都可用的动态 import() 。
导入代码是:
import { Injectable } from '@nestjs/common';
import { nanoid } from 'nanoid'; //wont import, gives error
import { User } from './data-objects/user.object';
Run Code Online (Sandbox Code Playgroud)
我还尝试了导入语句的变体:
// I tried this alternative
import * as nanoid from 'nanoid';
// Also tried this
const nanoid = require ( 'nanoid' );
Run Code Online (Sandbox Code Playgroud)
什么都没起作用。
为什么会发生此错误以及我应该如何在 NestJS 中安装 nanoid ?
我想获取一个ip来记录用户登录。
我尝试nestjs-real-ip过并且@Ip()。
我尝试通过手机和电脑浏览器登录,
但他们都表现出来了:::1。如何获取客户端的真实IP?
非常简单的一般问题:
@Controller('something')
class SomeController {
@Get()
foobar() {
return foo() // this returns a promise
}
}
Run Code Online (Sandbox Code Playgroud)
那么在这种情况下,我是否必须使foobar()控制器方法异步?我的理解是,这是没有必要的。NestJS 将自动解析返回的 Promise。仅当我想进入内部时才需要使方法异步await。
它是否正确?
这是我的存储库https://github.com/Shrinivassab/Portfolio
我正在尝试创建一个投资组合网站。当我尝试运行时vercel build
出现以下错误
Traced Next.js server files in: 1.490s
Created all serverless functions in: 788.915ms
Collected static files (public/, static/, .next/static): 4.085ms
Error: EPERM: operation not permitted, symlink 'getExperience.func' -> 'D:\Coding Examples\portfolio\.vercel\output\functions\api\getPageInfo.func'
Run Code Online (Sandbox Code Playgroud)
请问有什么建议吗?提前致谢
我有一个使用 NestJS 的项目。但是在尝试添加某些模块时它显示错误。
error TS2688: Cannot find type definition file for 'ioredis'.
The file is in the program because:
Entry point for implicit type library 'ioredis'
Run Code Online (Sandbox Code Playgroud)
这是我的 tsconfig
{
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"allowJs": true,
"outDir": "./dist",
"baseUrl": "./",
"paths": {
"@core/*": ["src/core/*"],
"@main/*": ["src/main/*"],
"@migrations/*": ["src/migrations/*"],
"@modules/*": ["src/modules/*"],
"@shared/*": ["src/shared/*"]
},
"incremental": true
},
"exclude": [
"node_modules", "dist"
]
}
Run Code Online (Sandbox Code Playgroud)
如何解决该错误?
我想在我的 NestJs 项目中执行这个 cron 函数:
@Cron('59 23 * * *')
async CashPendingCRON(){
let stores = await this.storeRepository.find();
for (let store of stores){
await this.connection
.createQueryBuilder()
.insert()
.into(CashPending)
.values([
{ cashPending: store.cashPending, store: store }
])
.execute()
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,玉米作业应该在每天晚上 11:59 执行。但它被执行了两次,并且条目被记录在数据库中两次。当我使用像 10 秒这样的间隔 (*/10 * * * * *) 时,它只会被调用一次。
如果有修复或者我做错了什么,请告诉我。
这是我在 app.module.ts 中添加 ScheduleModule 的方法
@Module({
imports: [
ScheduleModule.forRoot(),
ConfigModule.forRoot({
load: [appConfig, devConfig, stagConfig],
ignoreEnvFile: true,
isGlobal: true,
}),
TypeOrmModule.forRoot(
configService.getTypeOrmConfig(),
),
TypeOrmModule.forFeature([
User,
Vendor,
Store,
Product,
Category,
Brand,
AppVersion …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用内置 NestJS ValidationPipe 将一些查询参数从字符串转换为整数,但它似乎无法正常工作,
这是我的控制器:
import {
..., ValidationPipe
} from '@nestjs/common';
...
@UseGuards(JwtAuthGuard)
@Get()
findAll(@Req() req, @Query(new ValidationPipe({
transform: true,
transformOptions: { enableImplicitConversion: true },
forbidNonWhitelisted: true,
})) query: GetTestDto) {
return this.testService.findAll(query, req.user.id);
}
Run Code Online (Sandbox Code Playgroud)
这是我的 DTO :
import { IsInt, IsOptional, IsString } from 'class-validator';
import { Transform } from 'class-transformer';
export class GetTestDto {
@IsOptional()
@IsString()
public search: string;
@IsOptional()
@Transform(({value}) => {
console.log(typeof value); // => string / I'm just testing here
return value
})
@IsInt() …Run Code Online (Sandbox Code Playgroud) 我正在使用 Class-Validator 来验证 Nest.js 应用程序的 DTO 属性。在那里我有一个属性“images”,它是一个字符串数组,这些字符串是 URL。因此,我想验证该数组中的每个 URL。
class SomeDto {
// ...
// Array of Urls
@IsArray()
@IsUrl({each:true})
images: string[];
// ...
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。有谁知道如何验证这个 URL 数组。
在 Vercel 上部署后,我的 Nest.js 应用程序中的 POST 请求不起作用,并且收到 CORS 错误,但我的应用程序中的 cors 已启用,并且当我发送 GET 请求时,一切正常。当我在邮递员中测试我的请求时,一切正常。我不确定,但也许这个错误可能是由于我使用 React Query 或 vercel 问题而发生的,并且我没有正确部署我的应用程序。
我收到这样的错误:
从源“https://next-store-liard- Three.vercel.app”访问“https://server-store.vercel.app/api/auth/login”处的 XMLHttpRequest 已被 CORS 策略阻止:响应预检请求未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。
我启用了 CORS 这样的方法:
app.enableCors({
origin: ['http://localhost:3000', 'https://next-store-liard-three.vercel.app'],
allowedHeaders: ['Accept', 'Content-Type'],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
preflightContinue: false,
optionsSuccessStatus: 204,
credentials: true,
});
Run Code Online (Sandbox Code Playgroud)
这是我的文件 vercel.json:
{
"version": 2,
"name": "next-store-server",
"buildCommand": "npm start",
"installCommand": "npm install",
"builds": [
{
"src": "dist/main.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "dist/main.js",
"methods": ["GET", "POST", "PATCH", "PUT", "DELETE"]
} …Run Code Online (Sandbox Code Playgroud) nestjs ×10
javascript ×3
typescript ×3
dto ×2
node.js ×2
vercel ×2
asynchronous ×1
build ×1
controller ×1
cors ×1
cron ×1
deployment ×1
es6-modules ×1
ioredis ×1
ip ×1
nanoid ×1
reactjs ×1
tsconfig ×1