Skip to content

How to read request body from inside a Deno application? #12600

@goodmanworld

Description

@goodmanworld

I am trying to write a simple signup using Deno, and the following is my code:

async register(ctx: RouterContext) {
    const { email, password } = await ctx.request.body().value;
    let user = await User.findOne({ email });
    if (user) {
        ctx.response.status = 422;
        ctx.response.body = { message: "Email is already exist" };
        return;
    }
    const hashedPassword = hashSync(password);
    user = new User({ email, password: hashedPassword });
    await user.save();
    ctx.response.status = 201;
    ctx.response.body = {
        id: user.id,
        name: user.name,
        email: user.email
    };
}

The problem is that, what I get from the frontend side is like below:

{
    email: "<email>",
    password: "<password>"
}

But it seems my backend expects something like this:

{
    value: {
        email: "<email>",
        password: "<password>"
    }
}

And when I try to remove that .value part from the end of the following line of the code and change it like below:

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

It gives me error messages like:

Property 'email' does not exist on type 'Body'.
Property 'password' does not exist on type 'Body'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions