P
Prisma4w ago
Arima

<fields> is missing.

Created a simple post request that should create a user with a specific name, but I get an error
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}


model User {
id String @unique @default(cuid())
name String
@@map("users")
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}


model User {
id String @unique @default(cuid())
name String
@@map("users")
}
app.controllers.ts:
import { Body, Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service';

@Controller('users')
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello() {
this.appService.getHello().then(console.log);
}

@Post()
createUser(@Body("name") name: string) {
this.appService.createUser(name).then(console.log);
}
}
import { Body, Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service';

@Controller('users')
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello() {
this.appService.getHello().then(console.log);
}

@Post()
createUser(@Body("name") name: string) {
this.appService.createUser(name).then(console.log);
}
}
app.service.ts:
import { Injectable } from '@nestjs/common';
import PrismaService from './prisma.service';

@Injectable()
export class AppService {
constructor(
private prisma: PrismaService
) {}
async getHello() {
return this.prisma.user.count();
}

async createUser(name: string) {
return this.prisma.user.create({
data: {
name: name
}
});
}
}
import { Injectable } from '@nestjs/common';
import PrismaService from './prisma.service';

@Injectable()
export class AppService {
constructor(
private prisma: PrismaService
) {}
async getHello() {
return this.prisma.user.count();
}

async createUser(name: string) {
return this.prisma.user.create({
data: {
name: name
}
});
}
}
I don't even know what can cause it, I'm new to this field, but I read a couple of articles in detail on how to get started. But it didn't work. By the way, this request works properly! PrismaClientValidationError: Invalid this.prisma.user.create() invocation in C:\Users\reven\proj\src\app.service.ts:14:29 11 } 12 13 async createUser(name: string) { → 14 return this.prisma.user.create({ data: { + name: String } }) Argument name is missing.
5 Replies
Prisma AI Help
You chose to compile with the bot. One moment while I piece together your answer… Based on the information provided in your question and the knowledge sources available, it appears that you're encountering an error when trying to create a user using Prisma in a NestJS application. However, the specific error message is not provided in your question, which makes it challenging to pinpoint the exact issue. That being said, there are a few things we can check based on common issues: 1. Prisma Client Generation: Make sure you have generated the Prisma client after defining your schema. Run the following command:
npx prisma generate

npx prisma generate

2. PrismaService Implementation: Ensure that your PrismaService is correctly implemented. It should extend PrismaClient and connect to the database. Here's an example of how it should look:
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
}
}

import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
}
}

3. Module Configuration: Make sure that PrismaService is properly provided in your module (likely AppModule):
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaService } from './prisma.service';

@Module({
controllers: [AppController],
providers: [AppService, PrismaService],
})
export class AppModule {}

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaService } from './prisma.service';

@Module({
controllers: [AppController],
providers: [AppService, PrismaService],
})
export class AppModule {}

4. Error Handling: It's a good practice to add error handling to your service methods. This can help you identify the specific error you're encountering. For example:
async createUser(name: string) {
try {
return await this.prisma.user.create({
data: {
name: name
}
});
} catch (error) {
console.error('Error creating user:', error);
throw error;
}
}
async createUser(name: string) {
try {
return await this.prisma.user.create({
data: {
name: name
}
});
} catch (error) {
console.error('Error creating user:', error);
throw error;
}
}
If you're still encountering issues after checking these points, please provide the specific error message you're receiving. This will help in diagnosing the problem more accurately. For more information on setting up Prisma with NestJS, you can refer to the Building a REST API with NestJS and Prisma blog post. Citations: Building a REST API with NestJS and Prisma 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Arima
ArimaOP4w ago
No description
Arima
ArimaOP4w ago
But I do this:
this.appService.createUser(name ?? '123').then(console.log);
}
this.appService.createUser(name ?? '123').then(console.log);
}
this is working, something problem in postman?
{ id: 'cm6scwf0e0000wcj0xt65g761', name: '123' }
{ id: 'cm6scwf0e0000wcj0xt65g761', name: '123' }
Arima
ArimaOP4w ago
Oh..
No description
Arima
ArimaOP4w ago
And too work

Did you find this page helpful?