1

I am a new Deno learner following a tutorial video. The following code is a part of the tutorial's Authentication file code. As it's written by previous version of Deno has some errors that I could resolve most of them except the following errors:

import {
  create, verify, decode, getNumericDate, 
} from "../deps.ts";
import {RouterContext} from "https://deno.land/x/oak@v9.0.1/mod.ts";
import { userCollection } from "../mongo.ts";
import { hashSync, compareSync } from "../deps.ts";
import User from "../models/User.ts";

export class AuthController {
  async register(ctx: RouterContext) {
    const { value: { name, email, password } } = await ctx.request.body();

    let user = await User.findOne({ email });
    if (user) {
      ctx.response.status = 422;
      ctx.response.body = { message: "Email is already used" };
      return;
    }
    const hashedPassword = hashSync(password);
//////////////// FIRST ERROR HAPPENS HERE///////////////////
    user = new User({ name, email, password: hashedPassword }); 

    await user.save();
    ctx.response.status = 201;
    ctx.response.body = {
      id: user.id,
      name: user.name,
      email: user.email
    };
  }
  async login(ctx: RouterContext) {
/////////////////SECOND ERROR HAPPENS HERE /////////////////////
    const { value: { email, password } } = await ctx.request.body(); 
    if (!email || !password) {
      ctx.response.status = 422;
      ctx.response.body = { message: "Please provide email and password" };
      return;
    }
    let user = await User.findOne({ email });
    if (!user) {
      ctx.response.status = 422;
      ctx.response.body = { message: "Incorrect email" };
      return;
    }
    if (!compareSync(password, user.password)) {
      ctx.response.status = 422;
      ctx.response.body = { message: "Incorrect password" };
      return;
    }

    const key = await crypto.subtle.generateKey(
      { name: "HMAC", hash: "SHA-512" },
      true,
      ["sign", "verify"],
    );

    const jwt = create( { 
      alg: "HS256",
      typ: "JWT",
    }, {
      iss: user.email,
      exp: getNumericDate(
        Date.now() + parseInt(Deno.env.get("JWT_EXP_DURATION") || "0"))
    },
    key
    );

    ctx.response.body = {
      id: user.id,
      name: user.name,
      email: user.email,
      jwt,
    };
  }
}

export default new AuthController();

First error:

Property 'name' does not exist on type 'Promise<Uint8Array> | Promise<any> | Promise<URLSearchParams> | FormDataReader | Promise<string> | undefined'.deno-ts(2339) 

Property 'email' does not exist on type 'Promise<Uint8Array> | Promise<any> | Promise<URLSearchParams> | FormDataReader | Promise<string> | undefined'.deno-ts(2339) 

Property 'password' does not exist on type 'Promise<Uint8Array> | Promise<any> | Promise<URLSearchParams> | FormDataReader | Promise<string> | undefined'.deno-ts(2339)

Second error:

Property 'email' does not exist on type 'Promise<Uint8Array> | Promise<any> | Promise<URLSearchParams> | FormDataReader | Promise<string> | undefined'.deno-ts(2339) 

Property 'password' does not exist on type 'Promise<Uint8Array> | Promise<any> | Promise<URLSearchParams> | FormDataReader | Promise<string> | undefined'.deno-ts(2339)

I can't understand what does it mean and how can I fix them?

PS: The whole project code can be find here: https://github.com/thecodeholic/deno-survey

1 Answers1

1

Simply I added .value after the lines causing errors, and seems the problem resolved:

const { value: { name, email, password } } = await ctx.request.body().value;


const { value: { email, password } } = await ctx.request.body().value;