P
Prismaā€¢2w ago
Hano

Trying to create simple POST function (Expected double-quoted property name)

import prisma from '@/lib/prisma';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest) {
const data = await req.json();
const user = await prisma.user.create({
data,
});

return NextResponse.json(user);
}
import prisma from '@/lib/prisma';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest) {
const data = await req.json();
const user = await prisma.user.create({
data,
});

return NextResponse.json(user);
}
POST data:
{
"username": "bob",
"email": "[email protected]",
"password": "bob",
}
{
"username": "bob",
"email": "[email protected]",
"password": "bob",
}
Error:
SyntaxError: Expected double-quoted property name in JSON at position 80 (line 5 column 1)
at JSON.parse (<anonymous>)
at async POST (app\api\users\route.ts:14:15)
12 |
13 | export async function POST(req: NextRequest) {
> 14 | const data = await req.json();
| ^
15 | console.log(data);
16 | const user = await prisma.user.create({
17 | data,
POST /api/users 500 in 275ms
SyntaxError: Expected double-quoted property name in JSON at position 80 (line 5 column 1)
at JSON.parse (<anonymous>)
at async POST (app\api\users\route.ts:14:15)
12 |
13 | export async function POST(req: NextRequest) {
> 14 | const data = await req.json();
| ^
15 | console.log(data);
16 | const user = await prisma.user.create({
17 | data,
POST /api/users 500 in 275ms
4 Replies
Prisma AI Help
Prisma AI Helpā€¢2w ago
You've gone full bot mode! Let me fetch that AI answer for you ASAP.
Prisma AI Help
Prisma AI Helpā€¢2w ago
The error you're encountering is related to the JSON format of your POST data, not specifically to Prisma. However, I can help you resolve this issue as it's related to creating a user with Prisma. The error message "Expected double-quoted property name in JSON at position 80 (line 5 column 1)" suggests that there's an issue with the JSON format of your request body. Specifically, there's an extra comma after the last property in your JSON object, which is not valid JSON syntax. Here's the corrected JSON for your POST data:
{
"username": "bob",
"email": "[email protected]",
"password": "bob"
}
{
"username": "bob",
"email": "[email protected]",
"password": "bob"
}
Notice that I removed the trailing comma after the "password" field. In JSON, the last property in an object should not have a comma after it. Your Prisma POST function looks correct. However, you might want to add some error handling and validation. Here's a slightly improved version based on the knowledge sources:
import prisma from '@/lib/prisma';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest) {
try {
const data = await req.json();
const user = await prisma.user.create({
data: {
username: data.username,
email: data.email,
password: data.password
}
});

return NextResponse.json(user, { status: 201 });
} catch (error) {
console.error(error);
return NextResponse.json(
{ message: 'Something went wrong, please try again' },
{ status: 500 }
);
}
}
import prisma from '@/lib/prisma';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest) {
try {
const data = await req.json();
const user = await prisma.user.create({
data: {
username: data.username,
email: data.email,
password: data.password
}
});

return NextResponse.json(user, { status: 201 });
} catch (error) {
console.error(error);
return NextResponse.json(
{ message: 'Something went wrong, please try again' },
{ status: 500 }
);
}
}
This version includes error handling and explicitly specifies the fields to be created, which can help prevent unexpected data from being inserted into your database. Remember to hash the password before storing it in the database for security reasons. You can refer to the [Backend with TypeScript, PostgreSQL & Prisma: REST, Validation & Tests](<https://www.prisma.io/blog/backend-prisma-typescript-orm-with-postgresql-rest-api-validation-dcba1ps7kip3#defining
Prisma
TypeScript, PostgreSQL, Prisma Backend | REST API, Validation, Testing
This article is part of a series of live streams and articles on building a backend with TypeScript, PostgreSQL, and Prisma. In this article, we'll look at how to build a REST API, validate input, and testing the API.
Prisma AI Help
Prisma AI Helpā€¢2w ago
-the-create-user-route>) blog post for more detailed information on creating a user route with proper validation and password hashing. šŸ¤” 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.
Hano
HanoOPā€¢2w ago
Im really sorry, the bost resolved the issue, the problem was in the comma that I put during the post request. thank you

Did you find this page helpful?