Does this look correct? PUT Request..
Answered
Waterman posted this in #help-forum
WatermanOP
Hello, I might have used a little old method of making my put request because I am following a little old tutorial,
but does this put request look correct?
because I get an error and I have displayed it in the code and in the picture, overall could I improve some things?
Error:
Expected 0 arguments, but got 1.
Code:
but does this put request look correct?
because I get an error and I have displayed it in the code and in the picture, overall could I improve some things?
Error:
Expected 0 arguments, but got 1.
Code:
export const PUT = async (req: NextRequest, res: NextResponse) => {
const body: kropp = await req.json();
const { namn, fulltNamn, beskrivning, historia, kategori, pris, _id } = body;
await BasProdukt.findOneAndUpdate(
{ _id },
{ namn, fulltNamn, beskrivning, historia, kategori, pris }
);
res.json(true); <--- error message--- Expected 0 arguments, but got 1.
};Answered by Sphynx
@Waterman try
return NextResponse.json(true) instead of res.json(true)14 Replies
Mugger Crocodile
can you show the full code of the saveproduct function?
Original message was deleted
WatermanOP
oh ok, because my post request works fine but is it different for put request?
@Waterman oh ok, because my post request works fine but is it different for put request?
Mugger Crocodile
If the _id is false, the POST request will be executed. So, what kind of response do we get for any condition where _id is true?
WatermanOP
but the _id is not a boolean, or did I missunderstand your question
I did see though that the mongodb databse is being updated but why do I encounter a bunch of other errors? maybe thats what you trying to help with but just wanted to let you know 🙂
btw, the _id is the id of the product i am adding, then now trying to edit the product based on the id
@Waterman I did see though that the mongodb databse is being updated but why do I encounter a bunch of other errors? maybe thats what you trying to help with but just wanted to let you know 🙂
Mugger Crocodile
What I mean is that the _id is the if-else block. You said the post is working, which means it's skipping the put at that time. So, I want to see how the response from the post looks like when _id is there, and I also want to check for errors to see if they are related to API implementation or client error.
WatermanOP
this is one of the errors I get :
- error TypeError: Cannot read properties of undefined (reading 'headers')
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:277:61)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)Mugger Crocodile
Could you confirm if there's a part in your code where you access the undefined 'headers' property? Examining that section will help troubleshoot the issue
Regarding the error message you posted, it seems like you are accessing undefined headers. Could you please confirm if there is a part in your code where you are accessing the headers property?
Sphynx
you're using the new router, you're supposed to return the response and not res.something() afaik. also the headers error stacks back to next, which makes sense since you're not returning a response and next is probably trying to access
Sphynx
@Waterman try
return NextResponse.json(true) instead of res.json(true)Answer
@Mugger Crocodile Could you confirm if there's a part in your code where you access the undefined 'headers' property? Examining that section will help troubleshoot the issue
no, that's a nextjs bug, nextjs internally accesses the header but since you don't return any response, there are no headers to access