Mr.Bry
Mr.Bry5mo ago

[uncaught application error]: TypeError - ctx.request.body is not a function

.post("/savedata",async (ctx) => { if (!ctx.request.hasBody) { ctx.response.status = 400; ctx.response.body = { error: "No data provided" }; return; } try { const formData = await ctx.request.body({ type: "form-data" }); const CodeClient = formData.value.get("CodeClient"); const username = formData.value.get("username"); const password = formData.value.get("password"); const repeat_password= formData.value.get("repeatPassword"); const KodeActivation = formData.value.get("KodeActivation"); console.log(${username} ${password}); ctx.response.body = "Data saved"; } catch (error) { ctx.response.status = 500; ctx.response.body = { error: error.message }; } })
please help me😩
4 Replies
Deno AI Helper
Deno AI Helper5mo ago
It looks like you're encountering an error because ctx.request.body is not being called correctly. In the documentation for Oak, which is the middleware framework you're using, ctx.request.body() is indeed a function that you call to get the body of the request. However, you're trying to pass an object { type: "form-data" } to it, which might not be necessary or correct depending on the version of Oak you're using. Here's an example from the documentation on how to correctly use ctx.request.body():
.post("/users", async (ctx: Context) => {
const body = ctx.request.body();
const user = await body.value;
// ... rest of your code
})
.post("/users", async (ctx: Context) => {
const body = ctx.request.body();
const user = await body.value;
// ... rest of your code
})
In this example, ctx.request.body() is called without any arguments, and then await body.value is used to get the actual body content. For your case, if you're expecting form data, you might not need to specify { type: "form-data" } when calling ctx.request.body(). Instead, you can try the following:
const body = ctx.request.body(); // call without arguments
const formData = await body.value;
const body = ctx.request.body(); // call without arguments
const formData = await body.value;
If you're using a version of Oak that requires specifying the type, make sure that you're using the correct syntax and that your version of Oak supports it. Remember to check the version of Oak you're using and refer to the corresponding documentation for that version to ensure you're using the API correctly. If you're still having trouble, you might want to look at the Oak documentation or examples for handling form data in the version of Oak you're using. 🦕
Mr.Bry
Mr.BryOP5mo ago
I am using an EJS file, and I have an issue here. The problem is that I cannot retrieve the data filled in the form. How can I get the data from the form in my EJS file? My version oak 16.1.0 OMG, no body to help me
CatalinIuga
CatalinIuga5mo ago
I belive the way you are getting the request body is outdated: https://github.com/oakserver/oak/blob/f73348c78c6140e1251d20496262e29ed66741c8/CHANGELOG.md?plain=1#L189C6-L189C6 Here's how you parse the body as form data:
const formData = await ctx.request.body.formData();
const formData = await ctx.request.body.formData();
GitHub
oak/CHANGELOG.md at f73348c78c6140e1251d20496262e29ed66741c8 · oaks...
A middleware framework for handling HTTP with Deno, Node, Bun and Cloudflare Workers 🐿️ 🦕 - oakserver/oak
Mr.Bry
Mr.BryOP5mo ago
I solve it, thanks for the clue