1

I'm working as a beginner with Deno and the Opine framework. The first page is for login, but I can't read values of the form fields.

Form :

<form action="/signin" method="POST" style="margin-top:30px;">
    <input type="text" name="user" placeholder="Username" required="required" class="input-txt" value=""/>
    <p>&nbsp;</p>
    <input type="password" name="password" placeholder="Password" required="required" class="input-txt" />
    <div class="login-footer">
        <button type="submit" class="btn btn--right">Sign in  </button>
    </div>
</form>

Router in controller - my main problem is HERE. Can't understand how to read values of fields :

index.ts :

import { Router } from 'https://deno.land/x/opine/mod.ts';
const router = new Router();
router.post("/signin", async ( req, res ) => 
{
  console.log( "here... ???" ); 
})
export default router;

In server.ts:

import signin from "./controller/index.ts";
...
app.use( "/", signin );

I found many samples with OAK framework. In the post they use like that :

(ctx) => {
   const form = ctx.request.body ...

But the Opine framework seems to work in different way.

jps
  • 20,041
  • 15
  • 75
  • 79
Rimokas
  • 15
  • 1
  • 7

1 Answers1

2

First of all, I added the "use json" to my app[1]:

const app = opine();
const port = 3000;
app.use(urlencoded());

I then I parse the parameters here[2]:

Users.post("/", async (req, res) => {
  console.log(req.parsedBody.user)
  console.log(req.parsedBody.password)
}

Links for my personal project:

1 - https://github.com/ramonmedeiros/learning_deno/blob/master/opine/app.ts#L11

2 - https://github.com/ramonmedeiros/learning_deno/blob/master/opine/controllers/users.ts#L26-L54

Ramon Medeiros
  • 2,272
  • 2
  • 24
  • 41
  • Many thanks for the samples! Sorry, but unsucesful ... :(. I added json(). Thanks. Here is mine router : `router.post("/signin", async (req, res) => { const usr = req.parsedBody.user; const psw = req.parsedBody.password; console.log( usr, psw ); }); ` But it return error ` TypeError: Cannot read property 'user' of undefined at index.ts:7:30 at Layer.handle [as handle_request] (layer.ts:76:11) ` . Also I tried with `(await req.parsedBody.hasOwnProperty("user") == false) `, but starting server it throw error on 'hasOwnProperty' . What I'm missing ? – Rimokas Dec 01 '20 at 15:25
  • try to debug by only logging `req.parsedBody` – Ramon Medeiros Dec 01 '20 at 15:26
  • You may need to use the url encoded: https://github.com/asos-craigmorten/opine/blob/main/.github/API/request.md#reqbody – Ramon Medeiros Dec 01 '20 at 15:27
  • Wow! That is! url encoded did that! Many thanks ! – Rimokas Dec 01 '20 at 15:43