In a component when I press a button I make a post request to /api/Me:
const test = 'tsdf';
async function handleprisma() {
const res = await axios.post('/api/Me', {
firstName: '234',
lastName: 'sdfsdfsdfsfd',
id: 'fudck',
});
console.log(res.data);
}
But I want to do something like this:
const test = 'tsdf';
async function handleprisma() {
const res = await axios.post('/api/Me', {
firstName: test ,
lastName: 'sdfsdfsdfsfd',
id: 'fudck',
});
console.log(res.data);
}
Or like this:
const res = await axios.post('/api/Me', { test, context.something,
id: 'fudck', })
When I do that, it gives the following error:
AxiosError: Request failed with status code 500
What should I do?
/api/Me:
import { PrismaClient } from '@prisma/client';
import { useState, useEffect, useRef, useContext } from 'react';
const prisma = new PrismaClient();
export default async function handler(req, res) {
const { method } = req;
switch (method) {
case 'POST':
const { firstName, id, lastName } = req.body;
const Me = await prisma.Me.create({
data: {
firstName,
lastName,
id,
},
});
res.status(201).json(Me);
break;
default:
res.setHeader('Allow', ['POST']);
res.status(405).end(`Method ${method} Not Allowed`);
}
}
schema:
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
model Me{
id String @id @default (cuid())
firstName String
lastName String
}
I don't know how should I ask the question.
I think you should first check the API is working or not using postman and also check that the body requires form data or json body you can use this code simply and try this code where the variable name is same as the key in the json which is required
var firstName="abcd";
var lastName="xyz"
axios.post('/user', {
firstName,
lastName
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
You can refer here.