Why does (return new Response(({'error' : ""}), {status : code})) not work

return new Response(({'error' : "No ID provided"}), {status : 500})
return new Response(({'error' : "No ID provided"}), {status : 500})
3 Replies
akerbeltz
akerbeltz11mo ago
I think the problem is that the body of the Response can't be an object directly Based on mdn documentation you could create a Blob
const obj = { hello: "world" };
const myBlob = new Blob([JSON.stringify(obj, null, 2)], {
type: "application/json",
});
const myOptions = { status: 200, statusText: "SuperSmashingGreat!" };
const myResponse = new Response(myBlob, myOptions);
const obj = { hello: "world" };
const myBlob = new Blob([JSON.stringify(obj, null, 2)], {
type: "application/json",
});
const myOptions = { status: 200, statusText: "SuperSmashingGreat!" };
const myResponse = new Response(myBlob, myOptions);
akerbeltz
akerbeltz11mo ago
You could use another thing as the body parameter but if you want an object i think the best way is to create the blob But this is native javascript it is not part of the solid API